Removed app.param() callback with retval support
Before adoption: this commit came in with the history the project adopted, so no landing records it.
this is now abstract and will be going into express-params along with some other cases
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 4d12292 · Browse files · 3 files·+3 added−92 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+3 added−92 removed
- tests:+0 added−49 removed
- other:+3 added−43 removed
Provenance
- Commit
e78dc18- Tree
79259b6- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
4d12292
3 files changed+3 added−92 removed
modified:lib/router/index.js-16 removed
42 unmodified lines43 44 this.middleware = function(req, res, next){45 self._dispatch(req, res, next);46 };47-48- // treat functions with arity < 349- // to simply return a value50- this.param(function(name, fn){51- if (fn.length < 3) {52- return function(req, res, next){53- var val = req.params[name];54- val = req.params[name] = fn(val);55- if (invalidParamReturnValue(val)) {56- next('route');57- } else {58- next();59- }60- };61- }62- })47 }48 49 /**50 * Register a param callback `fn` for the given `name`.351 unmodified linesmodified:lib/http.js+3 added-27 removed
313 unmodified lines314 /**315 * Map the given param placeholder `name`(s) to the given callback `fn`.316 *317 * Param mapping is used to provide pre-conditions to routes318- * which us normalized placeholders. For example ":user_id" may319- * attempt to load the user from the database, where as ":num" may320- * pass the value through `parseInt(num, 10)`.318+ * which us normalized placeholders. This callback has the same319+ * signature as regular middleware, for example below when ":userId"320+ * is used this function will be invoked in an attempt to load the user.321 *322- * When the callback function accepts only a single argument, the323- * value of placeholder is passed:324- *325- * app.param('page', function(n){ return parseInt(n, 10); });326- *327- * If we do not with to supply the `parseInt` radix, we could simply do:328- *329- * app.param('page', parseInt);330- *331- * Or use `Number`:332- *333- * app.param('page', Number);334- *335- * After which "/users/:page" would automatically provide us with336- * an integer for `req.params.page`. If desired we could use the callback337- * signature shown below, and immediately `next(new Error('invalid page'))`338- * when `parseInt` fails.339- *340- * Alternatively the callback may accept the request, response, next, and341- * the value, acting like middlware:342- *322 * app.param('userId', function(req, res, next, id){323 * User.find(id, function(err, user){324 * if (err) {325 * next(err);5 unmodified lines331 * }332 * });333 * });334 *356- * Now every time ":userId" is present, the associated user object357- * will be loaded and assigned before the route handler is invoked.358- *335 * @param {String|Array|Function} name336 * @param {Function} fn337 * @return {Server} for chaining338 * @api public186 unmodified linesmodified:test/router.test.js-49 removed
115 unmodified lines116 , { name: 'jane' }117 , { name: 'bandit' }118 ];119 120- function integer(n){ return parseInt(n, 10); };121- app.param(['to', 'from'], integer);122-120 app.param('user', function(req, res, next, id){121 if (req.user = users[id]) {122 next();123 } else {4 unmodified lines128 app.get('/user/:user', function(req, res, next){129 res.send('user ' + req.user.name);130 });131 135- app.get('/users/:from-:to', function(req, res, next){136- var names = users.slice(req.params.from, req.params.to).map(function(user){137- return user.name;138- });139- res.send('users ' + names.join(', '));140- });141- 132 assert.response(app,133 { url: '/user/0' },134 { body: 'user tj' });135 136 assert.response(app,137 { url: '/user/1' },138 { body: 'user tobi' });149- 150- assert.response(app,151- { url: '/users/0-3' },152- { body: 'users tj, tobi, loki' });139 },140 155- 'test .param() ret val': function(){156- var app = express.createServer();157-158- app.param('uid', parseInt);159-160- app.get('/user/:uid', function(req, res, next){161- var id = req.params.uid;162- res.send('loaded user ' + id + ' as typeof ' + typeof id);163- });164-165- app.param('name', function(name){166- var captures;167- if (captures = /^\w[\w\d]+/.exec(name)) {168- return captures[0];169- }170- });171-172- app.get('/user/:name', function(req, res, next){173- var name = req.params.name;174- res.send('loaded user ' + name);175- });176-177- assert.response(app,178- { url: '/user/0' },179- { body: 'loaded user 0 as typeof number' });180-181- assert.response(app,182- { url: '/user/tj' },183- { body: 'loaded user tj' });184-185- assert.response(app,186- { url: '/user/t' },187- { status: 404 });188- },189- 141 'test OPTIONS': function(){142 var app = express.createServer();143 144 app.get('/', function(){});137 unmodified lines