Invoke router.param() only when route matches
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixes #2206
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent efbc3f9 · Browse files · 4 files·+56 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+56 added−3 removed
- tests:+25 added−0 removed
- other:+31 added−3 removed
Provenance
- Commit
269dc53- Tree
a193352- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
efbc3f9
4 files changed+56 added−3 removed
modified:lib/router/index.js+11 added-3 removed
189 unmodified lines190 if (err) {191 return next(err);192 }193 194- req.route = route;194+ var has_method = route._handles_method(method);195 196- // we can now dispatch to the route197- if (method === 'options' && !route.methods['options']) {196+ // build up automatic options response197+ if (!has_method && method === 'options') {198 options.push.apply(options, route._options());199 }200+201+ // don't even bother202+ if (!has_method && method !== 'head') {203+ return next();204+ }205+206+ // we can now dispatch to the route207+ req.route = route;208 }209 210 // Capture one-time layer values211 req.params = self.mergeParams308 unmodified linesmodified:lib/router/route.js+19 added
27 unmodified lines2829303132333435363738394041424344454647484950515253100 unmodified lines15415515615715815916016116225 unmodified lines27 unmodified lines this.methods = {};}
/** * @api private */
Route.prototype._handles_method = function _handles_method(method) { if (this.methods._all) { return true; }
method = method.toLowerCase();
if (method === 'head' && !this.methods['head']) { method = 'get'; }
return Boolean(this.methods[method]);};
/** * @return {Array} supported HTTP methods * @api private */
100 unmodified lines var msg = 'Route.all() requires callback functions but got a ' + type; throw new Error(msg); }
self.methods._all = true; self.stack.push({ handle: fn }); });
return self;25 unmodified linesmodified:test/app.param.js+25 added
175 unmodified lines176 .get('/bob')177 .expect('loki', done);178 })179 180+ it('should not invoke without route handler', function(done) {181+ var app = express();182+183+ app.param('thing', function(req, res, next, thing) {184+ req.thing = thing;185+ next();186+ });187+188+ app.param('user', function(req, res, next, user) {189+ next(new Error('invalid invokation'));190+ });191+192+ app.post('/:user', function(req, res, next) {193+ res.send(req.params.user);194+ });195+196+ app.get('/:thing', function(req, res, next) {197+ res.send(req.thing);198+ });199+200+ request(app)201+ .get('/bob')202+ .expect(200, 'bob', done);203+ })204+205 it('should work with encoded values', function(done){206 var app = express();207 208 app.param('name', function(req, res, next, name){78 unmodified linesmodified:History.md+1 added
9 unmodified lines10 - merges `req.params` from parent routes11 * deprecate things with `depd` module12 * fix behavior when handling request without routes13 * fix handling when `route.all` is only route14+ * invoke `router.param()` only when route matches15 * restore `req.params` after invoking router16 * use `finalhandler` for final response handling17 * use `media-typer` to alter content-type charset18 * deps: accepts@~1.0.71722 unmodified lines