Merge pull request #1964 from blakeembrey/modular-routing
Before adoption: this commit came in with the history the project adopted, so no landing records it.
keep route params from previous middleware matches
authored by Roman Shtylman · committed by Roman Shtylman · parents 3ea3250, c7e84d8 · Browse files · 7 files·+66 added−40 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+66 added−40 removed
- tests:+48 added−7 removed
- other:+18 added−33 removed
Provenance
- Commit
4867cf1- Tree
26ee98e- Author
Roman Shtylman <shtylman@gmail.com>- Committer
Roman Shtylman <shtylman@gmail.com>- Parent 1
3ea3250- Parent 2
c7e84d8
7 files changed+66 added−40 removed
modified:lib/router/index.js+13 added-28 removed
168 unmodified lines169 var route = layer.route;170 171 // if final route, then we support options172 if (route) {173- // we don't run any routs with error first173+ // we don't run any routes with error first174 if (err) {175 return next(err);176 }177 4 unmodified lines182 options.push.apply(options, route._options());183 }184 }185 186- req.params = layer.params;186+ req.params = req.params || {};187 188+ for (var key in layer.params) {189+ req.params[key] = layer.params[key];190+ }191+192 // this should be done for the layer193 return self.process_params(layer, req, res, function(err) {194 if (err) {195 return next(err);51 unmodified lines247 proto.process_params = function(route, req, res, done) {248 var params = this.params;249 250 // captured parameters from the route, keys and values247- var keys = route.keys || [];251+ var keys = route.keys;252 253 // fast track250- if (keys.length === 0) {254+ if (!keys || keys.length === 0) {255 return done();256 }257 258 var i = 0;59 unmodified lines318 * @api public319 */320 321 proto.use = function(route, fn){318-322 // default route to '/'323 if ('string' != typeof route) {324 fn = route;325 route = '/';9 unmodified lines335 if ('/' == route[route.length - 1]) {336 route = route.slice(0, -1);337 }338 336- var layer = Layer(route, {339+ var layer = new Layer(route, {340 sensitive: this.caseSensitive,341 strict: this.strict,342 end: false343 }, fn);20 unmodified lines364 365 proto.route = function(path){366 var route = new Route(path);367 365- var layer = Layer(path, {368+ var layer = new Layer(path, {369 sensitive: this.caseSensitive,370 strict: this.strict,371 end: true372 }, route.dispatch.bind(route));3 unmodified lines376 this.stack.push(layer);377 return route;378 };379 377-/**378- * Special-cased "all" method, applying the given route `path`,379- * middleware, and callback to _every_ HTTP method.380- *381- * @param {String} path382- * @param {Function} ...383- * @return {app} for chaining384- * @api public385- */386-387-proto.all = function(path, fn) {388- var route = this.route(path);389- methods.forEach(function(method){390- route[method](fn);391- });392-};393-380 // create Router#VERB functions395-methods.forEach(function(method){381+methods.concat('all').forEach(function(method){382 proto[method] = function(path, fn){397- var self = this;398- self.route(path)[method](fn);399- return self;383+ this.route(path)[method](fn);384+ return this;385 };386 });387 modified:lib/router/layer.js+2 added-2 removed
7 unmodified lines89101112131213141515 unmodified lines3132333435363738394020 unmodified lines7 unmodified lines }
debug('new %s', path); options = options || {}; this.path = path; this.params = {}; this.regexp = pathRegexp(path, this.keys = [], options); this.handle = fn;}
15 unmodified lines var val;
if (!m) return false;
this.path = m[0];
for (var i = 1, len = m.length; i < len; ++i) { key = keys[i - 1];
try {20 unmodified linesmodified:lib/router/route.js+2 added-2 removed
127 unmodified lines128 129 Route.prototype.all = function(fn){130 if (typeof fn !== 'function') {131 var type = {}.toString.call(fn);132- var msg = 'Route.use() requires callback functions but got a ' + type;132+ var msg = 'Route.all() requires callback functions but got a ' + type;133 throw new Error(msg);134 }135 136 if (!this.stack) {12 unmodified lines149 methods.forEach(function(method){150 Route.prototype[method] = function(fn){151 if (typeof fn !== 'function') {152 var type = {}.toString.call(fn);153- var msg = 'Route.use() requires callback functions but got a ' + type;153+ var msg = 'Route.' + method + '() requires callback functions but got a ' + type;154 throw new Error(msg);155 }156 157 debug('%s %s', method, this.path);16 unmodified linesmodified:test/app.route.js+13 added
34 unmodified lines35363738394041424344454647484950515234 unmodified lines request(app) .post('/foo') .expect('post', done); });
it('should support dynamic routes', function(done){ var app = express();
app.route('/:foo') .get(function(req, res) { res.send(req.params.foo); });
request(app) .get('/test') .expect('test', done); });});modified:test/app.use.js+15 added
61 unmodified lines62636465666768697071727374757677787980818261 unmodified lines
app.use('/blog', blog); blog.parent.should.equal(app); })
it('should support dynamic routes', function(done){ var blog = express() , app = express();
blog.get('/', function(req, res){ res.end(req.params.article); });
app.use('/post/:article', blog);
request(app) .get('/post/once-upon-a-time') .expect('once-upon-a-time', done); }) })})modified:test/Router.js+20 added-7 removed
3 unmodified lines4 , methods = require('methods')5 , assert = require('assert');6 7 describe('Router', function(){8-8 it('should return a function with router methods', function() {9 var router = Router();10 assert(typeof router == 'function');11 4 unmodified lines16 assert(typeof router.handle == 'function');17 assert(typeof router.use == 'function');18 });19 21- it('should support .use of other routers', function(done) {22- var router = Router();23- var another = Router();20+ it('should support .use of other routers', function(done){21+ var router = new Router();22+ var another = new Router();23 25- another.get('/bar', function(req, res) {26- res.done();24+ another.get('/bar', function(req, res){25+ res.end();26 });27 router.use('/foo', another);28 30- router.handle({ url: '/foo/bar', method: 'GET' }, { done: done });29+ router.handle({ url: '/foo/bar', method: 'GET' }, { end: done });30 });31 32+ it('should support dynamic routes', function(done){33+ var router = new Router();34+ var another = new Router();35+36+ another.get('/:bar', function(req, res){37+ req.params.foo.should.equal('test');38+ req.params.bar.should.equal('route');39+ res.end();40+ });41+ router.use('/:foo', another);42+43+ router.handle({ url: '/test/route', method: 'GET' }, { end: done });44+ });45+46 describe('.handle', function(){47 it('should dispatch', function(done){48 var router = new Router();49 120 unmodified linesmodified:package.json+1 added-1 removed
43 unmodified lines44 "utils-merge": "1.0.0",45 "escape-html": "1.0.1",46 "qs": "0.6.6",47 "serve-static": "1.0.1",48- "path-to-regexp": "0.1.0",48+ "path-to-regexp": "0.1.2",49 "debug": ">= 0.7.3 < 1"50 },51 "devDependencies": {52 "ejs": "~0.8.4",36 unmodified lines