Restore req.params after invoking Router
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixes #2163
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 746044b · Browse files · 3 files·+59 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+59 added−13 removed
- tests:+27 added−0 removed
- other:+32 added−13 removed
Provenance
- Commit
b89a597- Tree
2873a80- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
746044b
3 files changed+59 added−13 removed
modified:lib/router/index.js+31 added-13 removed
130 unmodified lines131 // middleware and routes132 var stack = self.stack;133 134 // manage inter-router variables135- var parent = req.next;135 var parentUrl = req.baseUrl || '';137- done = wrap(done, function(old, err) {138- req.baseUrl = parentUrl;139- req.next = parent;140- old(err);141- });136+ done = restore(done, req, 'baseUrl', 'next', 'params');137+138+ // setup next layer139 req.next = next;140 141 // for options requests, respond with a default if nothing else responds142 if (method === 'options') {113 unmodified lines256 next(err);257 }258 }259 }263-264- function wrap(old, fn) {265- return function () {266- var args = [old].concat(slice.call(arguments));267- fn.apply(this, args);268- };269- }260 };261 262 /**263 * Process any parameters for the layer.175 unmodified lines439 route[method].apply(route, [].slice.call(arguments, 1));440 return this;441 };442 });443+444+// restore obj props after function445+function restore(fn, obj) {446+ var props = new Array(arguments.length - 2);447+ var vals = new Array(arguments.length - 2);448+449+ for (var i = 0; i < props.length; i++) {450+ props[i] = arguments[i + 2];451+ vals[i] = obj[props[i]];452+ }453+454+ return function(err){455+ // restore vals456+ for (var i = 0; i < props.length; i++) {457+ obj[props[i]] = vals[i];458+ }459+460+ return fn.apply(this, arguments);461+ };462+}463+464+// wrap a function465+function wrap(old, fn) {466+ return function () {467+ var args = [old].concat(slice.call(arguments));468+ fn.apply(this, args);469+ };470+}modified:test/app.router.js+27 added
654 unmodified lines655 .get('/user/1')656 .expect(200, '0,1,2,3,4,5', done);657 })658 659+ it('should provide req.params to all handlers', function(done){660+ var app = express();661+ var router = new express.Router();662+663+ function handler1(req, res, next){664+ res.setHeader('x-user-id', req.params.id);665+ next()666+ }667+668+ function handler2(req, res){669+ res.send(req.params.id);670+ }671+672+ router.use(function(req, res, next){673+ res.setHeader('x-router', req.params.id);674+ next();675+ });676+677+ app.get('/user/:id', handler1, router, handler2);678+679+ request(app)680+ .get('/user/1')681+ .expect('x-router', 'undefined')682+ .expect('x-user-id', '1')683+ .expect(200, '1', done);684+ })685+686 it('should be chainable', function(){687 var app = express();688 app.get('/', function(){}).should.equal(app);689 })1 unmodified linemodified:History.md+1 added
1 unmodified line2 ==========3 4 * deprecate things with `depd` module5 * fix behavior when handling request without routes6+ * restore `req.params` after invoking router7 * use `finalhandler` for final response handling8 * use `media-typer` to alter content-type charset9 10 4.4.4 / 2014-06-201679 unmodified lines