Skip to content
Myr

Added Array support to 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 3e06586 · Browse files · 2 files+47 added−7 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+47 added−7 removed

  • tests:+1 added−2 removed
  • other:+46 added−5 removed

2 files+47 added−7 removed

modified:lib/express/server.js+46 added-5 removed
216 unmodified lines
217 return this;218};219220/**221 * Description221 * Map the given param placeholder `name`(s) to the given callback `fn`.222 *223 * @param {Type} name224 * @return {Type}223 * Param mapping is used to provide pre-conditions to routes224 * which us normalized placeholders. For example ":user_id" may225 * attempt to load the user from the database, where as ":num" may 226 * pass the value through `parseInt(num, 10)`.227 *228 * When the callback function accepts only a single argument, the229 * value of placeholder is passed:230 *231 * app.param('page', function(n){ return parseInt(n, 10); });232 *233 * After which "/users/:page" would automatically provide us with234 * an integer for `req.params.page`. If desired we could use the callback 235 * signature shown below, and immediately `next(new Error('invalid page'))`236 * when `parseInt` fails.237 *238 * Alternatively the callback may accept the request, response, next, and239 * the value, acting like middlware:240 *241 * app.param('userId', function(req, res, next, id){242 * User.find(id, function(err, user){243 * if (err) {244 * next(err);245 * } else if (user) {246 * req.user = user;247 * next();248 * } else {249 * next(new Error('failed to load user'));250 * }251 * });252 * });253 *254 * Now every time ":userId" is present, the associated user object255 * will be loaded and assigned before the route handler is invoked.256 *257 * @param {String|Array} name258 * @param {Function} fn259 * @return {Server} for chaining260 * @api public261 */262263Server.prototype.param = function(name, fn){264 if (Array.isArray(name)) {265 name.forEach(function(name){266 this.param(name, fn);267 }, this);268 } else {229 if (':' == name[0]) name = name.substr(1);230 this.routes.param(name, fn);269 if (':' == name[0]) name = name.substr(1);270 this.routes.param(name, fn);271 }272 return this;273};274275/**
144 unmodified lines
modified:test/express.test.js+1 added-2 removed
435 unmodified lines
436 , { name: 'bandit' }437 ];438439 function integer(n){ return parseInt(n, 10); };440 app.param(':from', integer);441 app.param('from', integer);440 app.param(['to', 'from'], integer);441442 app.param('user', function(req, res, next, id){443 if (req.user = users[id]) {444 next();
29 unmodified lines