Skip to content
Myr

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

7 files+66 added−40 removed

modified:lib/router/index.js+13 added-28 removed
168 unmodified lines
169 var route = layer.route;170171 // 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 lines
182 options.push.apply(options, route._options());183 }184 }185186 req.params = layer.params;186 req.params = req.params || {};187188 for (var key in layer.params) {189 req.params[key] = layer.params[key];190 }191192 // 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 lines
247proto.process_params = function(route, req, res, done) {248 var params = this.params;249250 // captured parameters from the route, keys and values247 var keys = route.keys || [];251 var keys = route.keys;252253 // fast track250 if (keys.length === 0) {254 if (!keys || keys.length === 0) {255 return done();256 }257258 var i = 0;
59 unmodified lines
318 * @api public319 */320321proto.use = function(route, fn){318322 // default route to '/'323 if ('string' != typeof route) {324 fn = route;325 route = '/';
9 unmodified lines
335 if ('/' == route[route.length - 1]) {336 route = route.slice(0, -1);337 }338336 var layer = Layer(route, {339 var layer = new Layer(route, {340 sensitive: this.caseSensitive,341 strict: this.strict,342 end: false343 }, fn);
20 unmodified lines
364365proto.route = function(path){366 var route = new Route(path);367365 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 lines
376 this.stack.push(layer);377 return route;378};379377/**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 */386387proto.all = function(path, fn) {388 var route = this.route(path);389 methods.forEach(function(method){390 route[method](fn);391 });392};393380// create Router#VERB functions395methods.forEach(function(method){381methods.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
modified:lib/router/route.js+2 added-2 removed
127 unmodified lines
128129Route.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 }135136 if (!this.stack) {
12 unmodified lines
149methods.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 }156157 debug('%s %s', method, this.path);
16 unmodified lines
modified:test/app.route.js+13 added
modified:test/app.use.js+15 added
modified:test/Router.js+20 added-7 removed
3 unmodified lines
4 , methods = require('methods')5 , assert = require('assert');67describe('Router', function(){88 it('should return a function with router methods', function() {9 var router = Router();10 assert(typeof router == 'function');11
4 unmodified lines
16 assert(typeof router.handle == 'function');17 assert(typeof router.use == 'function');18 });1921 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();2325 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);2830 router.handle({ url: '/foo/bar', method: 'GET' }, { done: done });29 router.handle({ url: '/foo/bar', method: 'GET' }, { end: done });30 });3132 it('should support dynamic routes', function(done){33 var router = new Router();34 var another = new Router();3536 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);4243 router.handle({ url: '/test/route', method: 'GET' }, { end: done });44 });4546 describe('.handle', function(){47 it('should dispatch', function(done){48 var router = new Router();49
120 unmodified lines
modified:package.json+1 added-1 removed
43 unmodified lines
44 "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