Skip to content
Myr

Merge branch 'master' of github.com:visionmedia/express

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 dd7af29, edfe50e · Browse files · 8 files+239 added−86 removed
This commit

State

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

Diff stats

8 files+239 added−86 removed

  • tests:+153 added−1 removed
  • other:+86 added−85 removed

8 files+239 added−86 removed

modified:lib/router/index.js+33 added-28 removed
25 unmodified lines
2627var methods = exports.methods = require('./methods');2829/**30 * Initialize a new `Router` with the given `app`.30 * Initialize a new `Router` with the given `options`.31 * 32 * @param {express.HTTPServer} app32 * @param {Object} options33 * @api private34 */3536function Router(app) {36function Router(options) {37 options = options || {};38 var self = this;38 this.app = app;39 this.routes = new Collection;40 this.map = {};41 this.params = {};42 this._params = [];43 this.caseSensitive = options.caseSensitive;44 this.strict = options.strict;4546 this.middleware = function(req, res, next){47 self._dispatch(req, res, next);48 };
92 unmodified lines
141 || method == 'all');142 });143};144143/**144 * Return routes with regexps that match the given `url`.145 *146 * @param {String} method147 * @param {String} url148 * @return {Collection}149 * @api public150 */145// /**146// * Return routes with regexps that match the given `url`.147// *148// * @param {String} method149// * @param {String} url150// * @return {Collection}151// * @api public152// */153// 154// Router.prototype.match = function(method, url){155// return this.find(function(route){156// return route.match(url)157// && (route.method == method158// || method == 'all');159// });160// };161152Router.prototype.match = function(method, url){153 return this.find(function(route){154 return route.match(url)155 && (route.method == method156 || method == 'all');157 });158};159162/**163 * Find routes based on the return value of `fn`164 * which is invoked once per route.165 *
50 unmodified lines
216 pass(req._route_index + 1, err);217 }218219 // match route218 req.route = route = self._match(req, i);220 req.route = route = self.match(req, i);221222 // implied OPTIONS223 if (!route && 'OPTIONS' == req.method) return self._options(req, res);224
98 unmodified lines
323};324325/**326 * Attempt to match a route for `req`325 * starting from offset `i`.327 * with optional starting index of `i`328 * defaulting to 0.329 *330 * @param {IncomingMessage} req331 * @param {Number} i332 * @return {Route}333 * @api private334 */335333Router.prototype._match = function(req, i, head){336Router.prototype.match = function(req, i, head){337 var method = req.method.toLowerCase()338 , url = parse(req.url)339 , path = url.pathname340 , routes = this.map341 , i = i || 0342 , captures343 , route344 , keys;345346 // HEAD support347 // TODO: clean this up348 if (!head && 'head' == method) {349 // attempt lookup346 route = this._match(req, i, true);350 route = this.match(req, i, true);351 if (route) return route;352353 // default to GET as res.render() / res.send()354 // etc support HEAD
40 unmodified lines
395 * @return {Router} for chaining396 * @api private397 */398395Router.prototype._route = function(method, path, callbacks){399Router.prototype.route = function(method, path, callbacks){400 var app = this.app401 , method = method.toLowerCase()402 , callbacks = utils.flatten([].slice.call(arguments, 2));403404 // ensure path was given400 if (!path) throw new Error('app.' + method + '() requires a path');405 if (!path) throw new Error('Router#' + method + '() requires a path');406407 // create the route408 var route = new Route(method, path, callbacks, {404 sensitive: app.enabled('case sensitive routing')405 , strict: app.enabled('strict routing')409 sensitive: this.caseSensitive410 , strict: this.strict411 });412413 // add it414 (this.map[method] = this.map[method] || []).push(route);
3 unmodified lines
modified:lib/application.js+4 added-35 removed
41 unmodified lines
42 var self = this;43 this.cache = {};44 this.settings = {};45 this.engines = {};46 this.isCallbacks = {};46 this.viewCallbacks = [];47 this.defaultConfiguration();4849 // route reflection methods
1 unmodified line
51 self.lookup[method] = function(path){52 return self._router.lookup(method, path);53 };5456 self.match[method] = function(path){57 return self._router.match(method, path);58 };5955 self.remove[method] = function(path){56 return self._router.lookup(method, path).remove();57 };58 });5960 // del -> delete61 self.lookup.del = self.lookup.delete;67 self.match.del = self.match.delete;62 self.remove.del = self.remove.delete;63};6465/**
35 unmodified lines
101 this._router = new Router(this);102 this.routes = this._router.routes;103 this.__defineGetter__('router', function(){104 this._usedRouter = true;105 this._router.caseSensitive = this.enabled('case sensitive routing');106 this._router.strict = this.enabled('strict routing');111 return self._router.middleware;107 return this._router.middleware;108 });109110 // default locals111 this.locals.settings = this.settings;
29 unmodified lines
141 return this._router.lookup('all', path);142};143144/**149 * Lookup routes matching the given `url`.150 *151 * @param {Stirng} url152 * @return {Array}153 * @api public154 */155156app.match = function(url){157 return this._router.match('all', url);158};159160/**145 * Proxy `connect#use()` to apply settings to146 * mounted applications.147 *148 * @param {String|Function|Server} route
141 unmodified lines
290 return this;291};292293/**310 * Register the given callback `fn` for the given `type`.311 *312 * @param {String} type313 * @param {Function} fn314 * @return {app} for chaining315 * @api public316 */317318app.is = function(type, fn){319 if (!fn) return this.isCallbacks[type];320 this.isCallbacks[type] = fn;321 return this;322};323324/**294 * Assign `setting` to `val`, or return `setting`'s value.295 * Mounted servers inherit their parent server's settings.296 *297 * @param {String} setting
136 unmodified lines
434 app[method] = function(path){435 if ('get' == method && 1 == arguments.length) return this.set(path); 436 var args = [method].concat([].slice.call(arguments));437 if (!this._usedRouter) this.use(this.router);469 return this._router._route.apply(this._router, args);438 return this._router.route.apply(this._router, args);439 }440});441442/**
84 unmodified lines
modified:lib/request.js+11 added-22 removed
265 unmodified lines
266 *267 * // With Content-Type: text/html; charset=utf-8268 * req.is('html');269 * req.is('text/html');270 * req.is('text/*');271 * // => true272 * 273 * // When Content-Type is application/json274 * req.is('json');275 * req.is('application/json');276 * req.is('application/*');277 * // => true278 * 279 * req.is('html');280 * // => false281 * 280 * Ad-hoc callbacks can also be registered with Express, to perform281 * assertions again the request, for example if we need an expressive282 * way to check if our incoming request is an image, we can register "an image"283 * callback:284 * 285 * app.is('an image', function(req){286 * return 0 == req.headers['content-type'].indexOf('image');287 * });288 *289 * app.is('an attachment', function(req){290 * return 0 == req.headers['content-disposition'].indexOf('attachment');291 * });292 *282 * Now within our route callbacks, we can use to to assert content types283 * such as "image/jpeg", "image/png", etc.284 * 285 * app.post('/image/upload', function(req, res, next){297 * if (req.is('an image')) {286 * if (req.is('image/*')) {287 * // do something288 * } else {289 * next();290 * }
4 unmodified lines
295 * @api public296 */297298req.is = function(type){310 var fn = this.app.is(type);311 if (fn) return fn(this);312 var contentType = this.headers['content-type'];313 if (!contentType) return;299 var ct = this.headers['content-type'];300 if (!ct) return false;301 ct = ct.split(';')[0];302 if (!~type.indexOf('/')) type = mime.lookup(type);303 if (~type.indexOf('*')) {304 type = type.split('/');317 contentType = contentType.split('/');318 if ('*' == type[0] && type[1] == contentType[1]) return true;319 if ('*' == type[1] && type[0] == contentType[0]) return true;305 ct = ct.split('/');306 if ('*' == type[0] && type[1] == ct[1]) return true;307 if ('*' == type[1] && type[0] == ct[0]) return true;308 return false;309 }321 return !! ~contentType.indexOf(type);310 return !! ~ct.indexOf(type);311};312313/**314 * Short-hand for `req.connection.encrypted`.
56 unmodified lines
added:test/req.is.js+100 added
12var express = require('../')3 , request = require('./support/http');45function req(ct) {6 var req = {7 headers: { 'content-type': ct }8 , __proto__: express.request9 };1011 return req;12}1314describe('req.is()', function(){15 it('should ignore charset', function(){16 req('application/json; charset=utf-8')17 .is('json')18 .should.be.true;19 })2021 describe('when content-type is not present', function(){22 it('should return false', function(){23 req('')24 .is('json')25 .should.be.false;26 })27 })2829 describe('when given an extension', function(){30 it('should lookup the mime type', function(){31 req('application/json')32 .is('json')33 .should.be.true;3435 req('text/html')36 .is('json')37 .should.be.false;38 })39 })4041 describe('when given a mime type', function(){42 it('should match', function(){43 req('application/json')44 .is('application/json')45 .should.be.true;4647 req('image/jpeg')48 .is('application/json')49 .should.be.false;50 })51 })5253 describe('when given */subtype', function(){54 it('should match', function(){55 req('application/json')56 .is('*/json')57 .should.be.true;5859 req('image/jpeg')60 .is('*/json')61 .should.be.false;62 })6364 describe('with a charset', function(){65 it('should match', function(){66 req('text/html; charset=utf-8')67 .is('*/html')68 .should.be.true;6970 req('text/plain; charset=utf-8')71 .is('*/html')72 .should.be.false;73 })74 })75 })7677 describe('when given type/*', function(){78 it('should match', function(){79 req('image/png')80 .is('image/*')81 .should.be.true;8283 req('text/html')84 .is('image/*')85 .should.be.false;86 })8788 describe('with a charset', function(){89 it('should match', function(){90 req('text/html; charset=utf-8')91 .is('text/*')92 .should.be.true;9394 req('something/html; charset=utf-8')95 .is('text/*')96 .should.be.false;97 })98 })99 })100})
added:test/Router.js+53 added
modified:test/utils.js-1 removed
added:client.js+25 added
12var http = require('http');34var times = 50;56while (times--) {7 var req = http.request({8 port: 30009 , method: 'POST'10 , headers: { 'Content-Type': 'application/x-www-form-urlencoded' }11 });1213 req.on('response', function(res){14 console.log(res.statusCode);15 });1617 var n = 500000;18 while (n--) {19 req.write('foo=bar&bar=baz&');20 }2122 req.write('foo=bar&bar=baz');2324 req.end();25}
added:test.js+13 added