Skip to content
Myr

Refactored Route to use a single array of callbacks

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 664907f · Browse files · 3 files+36 added−68 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+36 added−68 removed

  • tests:+1 added−2 removed
  • other:+35 added−66 removed

3 files+36 added−68 removed

modified:lib/router/index.js+31 added-60 removed
190 unmodified lines
191 */192193Router.prototype._dispatch = function(req, res, next){194 var params = this.params195 , self = this196 , error;195 , self = this;196197 // route dispatch199 (function pass(i){198 (function pass(i, err){199 var paramCallbacks200 , paramIndex = 0201 , paramVal202 , route
1 unmodified line
204 , key205 , ret;206207 // match next route209 function nextRoute() {210 pass(req._route_index + 1);208 function nextRoute(err) {209 pass(req._route_index + 1, err);210 }211212 // match route213 req.route = route = self._match(req, i);
1 unmodified line
215 // implied OPTIONS216 if (!route && 'OPTIONS' == req.method) return self._options(req, res);217218 // no route220 if (!route) return next(error);219 if (!route) return next(err);220221 // we have a route222 // start at param 0223 req.params = route.params;
4 unmodified lines
228 function param(err) {229 key = keys[i++];230 paramVal = key && req.params[key.name];231 paramCallbacks = key && params[key.name];233232 233 try {234 if ('route' == err) {235 nextRoute();236 } else if (err) {238 next(err);237 i = 0;238 callbacks(err);239 } else if (paramCallbacks && undefined !== paramVal) {240 paramCallback();241 } else if (key) {242 param();243 } else {244 i = 0;245 middleware();245 callbacks();246 }247 } catch (err) {248 next(err);248 param(err);249 }250 };251252 param();253252 param(err);253 254 // single param callbacks255 function paramCallback(err) {256 var fn = paramCallbacks[paramIndex++];257 if (err || !fn) return param(err);258 fn(req, res, paramCallback, paramVal, key.name);259 }260261 // invoke route middleware262 function middleware(err) {263 var fn = route.middleware[i++];264 if ('route' == err) {265 nextRoute();266 } else if (err) {267 next(err);268 } else if (fn) {269 fn(req, res, middleware);270 } else {271 done();272 }273 };274275 // invoke middleware callback276 function done() {277 var fn = route.callback;278261 // invoke route callbacks262 function callbacks(err) {263 var fn = route.callbacks[i++];264 try {280 if (error) {281 if (fn.length < 4) return nextRoute();282 fn(error, req, res, function(err){283 error = err;284 nextRoute();285 });265 if ('route' == err) {266 nextRoute();267 } else if (err && fn) {268 if (fn.length < 4) return callbacks(err);269 fn(err, req, res, callbacks);270 } else if (fn) {271 fn(req, res, callbacks);272 } else {287 fn(req, res, function(err){288 error = err;289 nextRoute();273 nextRoute(err);290 });274 }275 } catch (err) {293 error = err;294 nextRoute();276 callbacks(err);277 }278 }279 })(0);280};
86 unmodified lines
367 }368};369370/**389 * Route `method`, `path`, and optional middleware390 * to the callback `fn`.371 * Route `method`, `path`, and one or more callbacks.372 *373 * @param {String} method374 * @param {String} path394 * @param {Function} ...395 * @param {Function} fn375 * @param {Function} callback...376 * @return {Router} for chaining377 * @api private378 */379400Router.prototype._route = function(method, path, fn){380Router.prototype._route = function(method, path, callbacks){381 var app = this.app402 , middleware = [];382 , callbacks = utils.flatten(toArray(arguments, 2));383404 // slice middleware405 if (arguments.length > 3) {406 middleware = toArray(arguments, 2);407 fn = middleware.pop();408 middleware = utils.flatten(middleware);409 }384 // ensure path was given385 if (!path) throw new Error('app.' + method + '() requires a path');386411 // ensure path and callback are given412 if (!path) throw new Error(method + 'route requires a path');413 if (!fn) throw new Error(method + ' route ' + path + ' requires a callback');414387 // create the route416 var route = new Route(method, path, fn, {388 var route = new Route(method, path, callbacks, {389 sensitive: app.enabled('case sensitive routes')390 , strict: app.enabled('strict routing')419 , middleware: middleware391 });392393 // add it394 (this.routes[method] = this.routes[method] || [])
3 unmodified lines
modified:lib/router/route.js+4 added-6 removed
modified:test/router.test.js+1 added-2 removed
518 unmodified lines
519 app.get('/user/:id/edit', function(){});520 521 var route = app.get('/user/:id')[0]522 route.should.be.an.instanceof(Route);523 route.callback.should.be.a('function');523 route.callbacks.should.be.an.instanceof(Array);524 route.path.should.equal('/user/:id');525 route.regexp.should.be.an.instanceof(RegExp);526 route.method.should.equal('get');527 route.keys.should.eql([{ name: 'id', optional: false }]);
53 unmodified lines
581 app.get('/user/:id/edit', function(){});582 583 var route = app.match.get('/user/12')[0];584 route.should.be.an.instanceof(Route);585 route.callback.should.be.a('function');585 route.path.should.equal('/user/:id');586 route.regexp.should.be.an.instanceof(RegExp);587 route.method.should.equal('get');588 route.keys.should.eql([{ name: 'id', optional: false }]);
217 unmodified lines