Skip to content
Myr

Added Server#param()

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

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 60f1483 · Browse files · 2 files+61 added−0 removed
This commit

State

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

Diff stats

2 files+61 added−0 removed

  • tests:+47 added−0 removed
  • other:+14 added−0 removed

2 files+61 added−0 removed

modified:lib/express/server.js+14 added
216 unmodified lines
217 return this;218};219220/**221 * Description222 *223 * @param {Type} name224 * @return {Type}225 * @api public226 */227228Server.prototype.param = function(name, fn){229 if (':' == name[0]) name = name.substr(1);230 this.routes.param(name, fn);231 return this;232};233234/**235 * Assign a custom exception handler callback `fn`.236 * These handlers are always _last_ in the middleware stack.237 *238 * @param {Function} fn
140 unmodified lines
modified:test/express.test.js+47 added
421 unmodified lines
422 assert.response(app,423 { url: '/' + thing, headers: { 'X-Role': 'member', 'X-Age': 18 }},424 { body: 'OK', status: 200 });425 });426 },427 428 'test .param()': function(){429 var app = express.createServer();430431 var users = [432 { name: 'tj' }433 , { name: 'tobi' }434 , { name: 'loki' }435 , { name: 'jane' }436 , { name: 'bandit' }437 ];438439 function integer(n){ return parseInt(n, 10); };440 app.param(':from', integer);441 app.param('from', integer);442443 app.param('user', function(req, res, next, id){444 if (req.user = users[id]) {445 next();446 } else {447 next(new Error('failed to find user'));448 }449 });450451 app.get('/user/:user', function(req, res, next){452 res.send('user ' + req.user.name);453 });454455 app.get('/users/:from-:to', function(req, res, next){456 var names = users.slice(req.params.from, req.params.to).map(function(user){457 return user.name;458 });459 res.send('users ' + names.join(', '));460 });461 462 assert.response(app,463 { url: '/user/0' },464 { body: 'user tj' });465 466 assert.response(app,467 { url: '/user/1' },468 { body: 'user tobi' });469 470 assert.response(app,471 { url: '/users/0-3' },472 { body: 'users tj, tobi, loki' });473 }474};