Skip to content
Myr

docs

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 b1a7310 · Browse files · 1 file+21 added−13 removed
This commit

State

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

Diff stats

1 file+21 added−13 removed

  • other:+21 added−13 removed

1 file+21 added−13 removed

modified:lib/http.js+21 added-13 removed
320 unmodified lines
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); });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 *335 * app.param('userId', function(req, res, next, id){336 * User.find(id, function(err, user){337 * if (err) {338 * next(err);339 * } else if (user) {340 * req.user = user;341 * next();342 * } else {343 * next(new Error('failed to load user'));344 * }345 * });346 * });343 * app.param('userId', function(req, res, next, id){344 * User.find(id, function(err, user){345 * if (err) {346 * next(err);347 * } else if (user) {348 * req.user = user;349 * next();350 * } else {351 * next(new Error('failed to load user'));352 * }353 * });354 * });355 *356 * Now every time ":userId" is present, the associated user object357 * will be loaded and assigned before the route handler is invoked.358 *
190 unmodified lines