Restore original `res.app` when out() is called.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ben Weaver · committed by Tj Holowaychuk · parent 80f4d08 · Browse files · 1 file·+30 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+30 added−6 removed
- other:+30 added−6 removed
Provenance
- Commit
f12baf3- Tree
9b4a19c- Author
Ben Weaver <ben@orangesoda.net>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
80f4d08
1 file changed+30 added−6 removed
modified:lib/http.js+30 added-6 removed
49 unmodified lines50 this.viewHelpers = {};51 this.dynamicViewHelpers = {};52 this.errorHandlers = [];53 54- // default "home" to / 54+ // default "home" to /55 this.set('home', '/');56 57 // set "env" to NODE_ENV, defaulting to "development"58 this.set('env', process.env.NODE_ENV || 'development');73 unmodified lines132 * @api public133 */134 135 Server.prototype.use = function(route, middleware){136- var app, home;136+ var app, home, handle;137 138 if ('string' != typeof route) {139 middleware = route, route = '/';140 }141 142- connect.HTTPServer.prototype.use.call(this, route, middleware);142+ // Bind a handler for nested server middleware. This logic is lifted143+ // out of connect's Server.prototype.use() and modified to add a144+ // custom `out` callback.145+ //146+ // The `res.app` and `req.app` properties are set by the first entry147+ // in the middleware stack (see Server() constructor). When a nested148+ // server context is entered, the `.app` property is changed, but149+ // it's not restored when `.handle()` calls `out()`.150+ //151+ // A different approach might be to add `begin`, `end`, `suspend`,152+ // and `resume` events to servers.153 154+ handle = middleware;155+ if (middleware.handle) {156+ middleware.route = route;157+ handle = function(req, res, next) {158+ var origApp = res.app;159+ middleware.handle(req, res, next && function() {160+ req.app = res.app = origApp;161+ return next.apply(this, arguments);162+ });163+ };164+ }165+166+ connect.HTTPServer.prototype.use.call(this, route, handle);167+168 // mounted an app169 if (middleware.handle) {170 app = middleware;171 // express app56 unmodified lines228 * @return {Server} for chaining229 * @api public230 */231 208-Server.prototype.helpers = 232+Server.prototype.helpers =233 Server.prototype.locals = function(obj){234 utils.merge(this.viewHelpers, obj);235 return this;236 };16 unmodified lines253 * Map the given param placeholder `name`(s) to the given callback `fn`.254 *255 * Param mapping is used to provide pre-conditions to routes256 * which us normalized placeholders. For example ":user_id" may233- * attempt to load the user from the database, where as ":num" may 257+ * attempt to load the user from the database, where as ":num" may258 * pass the value through `parseInt(num, 10)`.259 *260 * When the callback function accepts only a single argument, the261 * value of placeholder is passed:262 *263 * app.param('page', function(n){ return parseInt(n, 10); });264 *265 * After which "/users/:page" would automatically provide us with242- * an integer for `req.params.page`. If desired we could use the callback 266+ * an integer for `req.params.page`. If desired we could use the callback267 * signature shown below, and immediately `next(new Error('invalid page'))`268 * when `parseInt` fails.269 *270 * Alternatively the callback may accept the request, response, next, and207 unmodified lines