Skip to content
Myr

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

3 files+59 added−13 removed

modified:lib/router/index.js+31 added-13 removed
130 unmodified lines
131 // middleware and routes132 var stack = self.stack;133134 // 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');137138 // setup next layer139 req.next = next;140141 // for options requests, respond with a default if nothing else responds142 if (method === 'options') {
113 unmodified lines
256 next(err);257 }258 }259 }263264 function wrap(old, fn) {265 return function () {266 var args = [old].concat(slice.call(arguments));267 fn.apply(this, args);268 };269 }260};261262/**263 * Process any parameters for the layer.
175 unmodified lines
439 route[method].apply(route, [].slice.call(arguments, 1));440 return this;441 };442});443444// restore obj props after function445function restore(fn, obj) {446 var props = new Array(arguments.length - 2);447 var vals = new Array(arguments.length - 2);448449 for (var i = 0; i < props.length; i++) {450 props[i] = arguments[i + 2];451 vals[i] = obj[props[i]];452 }453454 return function(err){455 // restore vals456 for (var i = 0; i < props.length; i++) {457 obj[props[i]] = vals[i];458 }459460 return fn.apply(this, arguments);461 };462}463464// wrap a function465function 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 lines
655 .get('/user/1')656 .expect(200, '0,1,2,3,4,5', done);657 })658659 it('should provide req.params to all handlers', function(done){660 var app = express();661 var router = new express.Router();662663 function handler1(req, res, next){664 res.setHeader('x-user-id', req.params.id);665 next()666 }667668 function handler2(req, res){669 res.send(req.params.id);670 }671672 router.use(function(req, res, next){673 res.setHeader('x-router', req.params.id);674 next();675 });676677 app.get('/user/:id', handler1, router, handler2);678679 request(app)680 .get('/user/1')681 .expect('x-router', 'undefined')682 .expect('x-user-id', '1')683 .expect(200, '1', done);684 })685686 it('should be chainable', function(){687 var app = express();688 app.get('/', function(){}).should.equal(app);689 })
1 unmodified line
modified:History.md+1 added
1 unmodified line
2==========34 * 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 charset9104.4.4 / 2014-06-20
1679 unmodified lines