Invoke router.param() only when route matches

fixes #2206
This commit is contained in:
Douglas Christopher Wilson 2014-07-03 16:41:23 -04:00
parent efbc3f95ee
commit 269dc5323f
4 changed files with 56 additions and 3 deletions

View File

@ -11,6 +11,7 @@ unreleased
* deprecate things with `depd` module
* fix behavior when handling request without routes
* fix handling when `route.all` is only route
* invoke `router.param()` only when route matches
* restore `req.params` after invoking router
* use `finalhandler` for final response handling
* use `media-typer` to alter content-type charset

View File

@ -191,12 +191,20 @@ proto.handle = function(req, res, done) {
return next(err);
}
req.route = route;
var has_method = route._handles_method(method);
// we can now dispatch to the route
if (method === 'options' && !route.methods['options']) {
// build up automatic options response
if (!has_method && method === 'options') {
options.push.apply(options, route._options());
}
// don't even bother
if (!has_method && method !== 'head') {
return next();
}
// we can now dispatch to the route
req.route = route;
}
// Capture one-time layer values

View File

@ -28,6 +28,24 @@ function Route(path) {
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
@ -137,6 +155,7 @@ Route.prototype.all = function(){
throw new Error(msg);
}
self.methods._all = true;
self.stack.push({ handle: fn });
});

View File

@ -177,6 +177,31 @@ describe('app', function(){
.expect('loki', done);
})
it('should not invoke without route handler', function(done) {
var app = express();
app.param('thing', function(req, res, next, thing) {
req.thing = thing;
next();
});
app.param('user', function(req, res, next, user) {
next(new Error('invalid invokation'));
});
app.post('/:user', function(req, res, next) {
res.send(req.params.user);
});
app.get('/:thing', function(req, res, next) {
res.send(req.thing);
});
request(app)
.get('/bob')
.expect(200, 'bob', done);
})
it('should work with encoded values', function(done){
var app = express();