Merge branch 'params' into connect
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 · parents 7897460, 046172e · Browse files · 3 files·+55 added−2 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+55 added−2 removed
- tests:+28 added−1 removed
- other:+27 added−1 removed
Provenance
- Commit
9449c4b- Tree
6afbf7b- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
7897460- Parent 2
046172e
3 files changed+55 added−2 removed
modified:lib/express/request.js+16 added
36 unmodified lines37383940414243444546474849505152535455565758596011 unmodified lines36 unmodified lineshttp.IncomingMessage.prototype.header = function(name, defaultValue){ return this.headers[name.toLowerCase()] || defaultValue;};
http.IncomingMessage.prototype.param = function(name){ // Route params like /user/:id if (this.params.path[name] !== undefined) { return this.params.path[name]; } // Query string params if (this.params.get[name] !== undefined) { return this.params.get[name]; } // POST params via connect.bodyDecoder if (this.body && this.body[name] !== undefined) { return this.body[name]; }};
// Callback for isXMLHttpRequest / xhr
function isxhr() { return this.header('X-Requested-With', '').toLowerCase() === 'xmlhttprequest';11 unmodified linesmodified:lib/express/server.js+11 added-1 removed
8 unmodified lines9 * Module dependencies.10 */11 12 var sys = require('sys'),13+ url = require('url'),14 connect = require('connect'),15+ queryString = require('querystring'),16 router = require('connect/middleware/router');17 18 /**19 * Initialize a new `Server` with optional `middleware`.7 unmodified lines27 this.config = {};28 this.settings = {};29 connect.Server.call(this, middleware || []);30 29- // Expose app to extensions31+ // Expose objects to each other32 this.use('/', function(req, res, next){33+ req.params = req.params || {};34+ req.params.get = {};35 res.headers = {};36 req.app = res.app = self;37 req.res = res;38 res.req = req;39+ // Assign req.params.get40+ if (req.url.indexOf('?') > 0) {41+ // TODO: consider simple substr to increase performance42+ var query = url.parse(req.url).query;43+ req.params.get = queryString.parse(query);44+ }45 next();46 });47 48 // Use router, expose as app.get(), etc110 unmodified linesmodified:test/request.test.js+28 added-1 removed
1234566789101125 unmodified lines3738394041424344454647484950515253545556575859606162636465666768
/** * Module dependencies. */
var express = require('express');var express = require('express'), connect = require('connect');
module.exports = { '#isXMLHttpRequest': function(assert){ var app = express.createServer();25 unmodified lines assert.response(app, { url: '/', headers: { Host: 'foo.com' }}, { body: 'wahoo' }); }, '#param()': function(assert){ var app = express.createServer( connect.bodyDecoder() ); app.get('/user/:id?', function(req, res){ res.send('user ' + req.param('id')); }); app.post('/user', function(req, res){ res.send('user ' + req.param('id')); }); assert.response(app, { url: '/user/12' }, { body: 'user 12' }); assert.response(app, { url: '/user?id=5' }, { body: 'user 5' }); assert.response(app, { url: '/user', method: 'POST', data: 'id=1', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }}, { body: 'user 1' }); }};No newline at end of fileNo newline at end of file