Skip to content
Myr

Refactored Server#use()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

eventually we should just emit some events from connect

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent c9f5bb6 · Browse files · 1 file+18 added−31 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+18 added−31 removed

  • other:+18 added−31 removed

1 file+18 added−31 removed

modified:lib/http.js+18 added-31 removed
137 unmodified lines
138 if ('string' != typeof route) {139 middleware = route, route = '/';140 }141142 // 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.142 // express app143 if (middleware.handle && middleware.set) app = middleware;144154 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, function(err) {160 req.app = res.app = origApp;161 return next(err);145 // restore .app property on req and res146 if (app) {147 app.route = route;148 middleware = function(req, res, next) {149 var orig = req.app;150 app.handle(req, res, function(err){151 req.app = res.app = orig;152 next(err);153 });154 };155 }156166 connect.HTTPServer.prototype.use.call(this, route, handle);157 connect.HTTPServer.prototype.use.call(this, route, middleware);158168 // mounted an app169 if (middleware.handle) {170 app = middleware;171 // express app172 if (app.set) {173 home = app.set('home');174 if ('/' == home) home = '';175 app.set('home', app.route + home);176 app.parent = this;159 // mounted an app, invoke the hook160 // and adjust some settings161 if (app) {162 home = app.set('home');163 if ('/' == home) home = '';164 app.set('home', app.route + home);165 app.parent = this;177 }178 // mounted hook166 if (app.__mounted) app.__mounted.call(app, this);167 }168169 return this;
295 unmodified lines