Skip to content

Commit a26ccde

Browse files
committed
Make LocalApp#require asynchronous.
The implementation is still synchronous, but the signature looks async.
1 parent 3812fb3 commit a26ccde

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

coder-apps/common/coderlib/app/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ LocalApp.prototype.load = function(callback) {
7474
});
7575
};
7676

77-
LocalApp.prototype.require = function() {
78-
return require(this.loadpath);
77+
LocalApp.prototype.require = function(callback) {
78+
callback(null, require(this.loadpath));
7979
};
8080

8181
LocalApp.prototype.view = function(name) {

coder-base/server.js

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ var apphandler = function( req, res, appdir ) {
5959
});
6060
return;
6161
}
62-
userapp = app.require();
6362

6463
res.locals["app_name"] = appname;
6564
res.locals["app_url"] = "/app/" + appname;
@@ -76,40 +75,47 @@ var apphandler = function( req, res, appdir ) {
7675
return;
7776
}
7877

79-
var routes = [];
80-
if ( req.route.method === 'get' ) {
81-
routes = userapp.get_routes;
82-
} else if ( req.route.method === 'post' ) {
83-
routes = userapp.post_routes;
84-
}
78+
app.require(function(err, userapp) {
79+
if (err) {
80+
res.send(500);
81+
return;
82+
}
83+
84+
var routes = [];
85+
if ( req.route.method === 'get' ) {
86+
routes = userapp.get_routes;
87+
} else if ( req.route.method === 'post' ) {
88+
routes = userapp.post_routes;
89+
}
90+
91+
if ( routes ) {
92+
var found = false;
93+
for ( var i in routes ) {
94+
route = routes[i];
95+
if ( route['path'] instanceof RegExp ) {
96+
var m = route['path'].exec( apppath );
97+
if ( m ) {
98+
userapp[route['handler']]( app, req, res, m );
99+
found = true;
100+
break;
101+
}
85102

86-
if ( routes ) {
87-
var found = false;
88-
for ( var i in routes ) {
89-
route = routes[i];
90-
if ( route['path'] instanceof RegExp ) {
91-
var m = route['path'].exec( apppath );
92-
if ( m ) {
93-
userapp[route['handler']]( app, req, res, m );
103+
} else if ( route['path'] === apppath ) {
104+
userapp[route['handler']]( app, req, res );
94105
found = true;
95106
break;
96107
}
97108

98-
} else if ( route['path'] === apppath ) {
99-
userapp[route['handler']]( app, req, res );
100-
found = true;
101-
break;
102109
}
103110

111+
if ( !found ) {
112+
res.status( 404 );
113+
res.render('404', {
114+
title: 'error'
115+
});
116+
}
104117
}
105-
106-
if ( !found ) {
107-
res.status( 404 );
108-
res.render('404', {
109-
title: 'error'
110-
});
111-
}
112-
}
118+
});
113119
});
114120
};
115121

0 commit comments

Comments
 (0)