Skip to content
Myr

Added default param support to req.param(name, default)

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 8e63d04 · Browse files · 3 files+11 added−5 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+11 added−5 removed

  • tests:+5 added−1 removed
  • other:+6 added−4 removed

3 files+11 added−5 removed

modified:docs/guide.md+2 added-2 removed
683 unmodified lines
684We may also assert the _type_ as shown below, which would return true for _"application/json"_, and _"text/json"_.685686 req.is('*/json');687688### req.param(name)688### req.param(name[, default])689690Return the value of param _name_ when present.690Return the value of param _name_ when present or _default_.691692 - Checks route placeholders (_req.params_), ex: /user/:id693 - Checks query string params (_req.query_), ex: ?id=12694 - Checks urlencoded body params (_req.body_), ex: id=12
415 unmodified lines
modified:lib/express/request.js+4 added-2 removed
107 unmodified lines
108 }109};110111/**112 * Return the value of param `name` when present.112 * Return the value of param `name` when present or `defaultValue`.113 *114 * - Checks route placeholders, ex: _/user/:id_115 * - Checks query string params, ex: ?id=12116 * - Checks urlencoded body params, ex: id=12
2 unmodified lines
119 * should be an object. This can be done by using120 * the `connect.bodyDecoder` middleware.121 *122 * @param {String} name123 * @param {Mixed} defaultValue124 * @return {String}125 * @api public126 */127127http.IncomingMessage.prototype.param = function(name){128http.IncomingMessage.prototype.param = function(name, defaultValue){129 // Route params like /user/:id130 if (this.params[name] !== undefined) {131 return this.params[name]; 132 }
4 unmodified lines
137 // Request body params via connect.bodyDecoder138 if (this.body && this.body[name] !== undefined) {139 return this.body[name];140 }141 return defaultValue;142};143144/**145 * Queue flash `msg` of the given `type`.
132 unmodified lines
modified:test/request.test.js+5 added-1 removed
98 unmodified lines
99 connect.bodyDecoder()100 );101 102 app.get('/user/:id?', function(req, res){103 res.send('user ' + req.param('id'));103 res.send('user ' + req.param('id', 'unknown'));104 });105 106 app.post('/user', function(req, res){107 res.send('user ' + req.param('id'));
7 unmodified lines
115 { url: '/user?id=5' },116 { body: 'user 5' });117 118 assert.response(app,119 { url: '/user' },120 { body: 'user unknown' });121 122 assert.response(app,123 { url: '/user', method: 'POST', data: 'id=1', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }},124 { body: 'user 1' });125 },126
149 unmodified lines