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
Provenance
- Commit
75054e1- Tree
a0fea9d- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
664907f
3 files changed+36 added−68 removed
modified:lib/router/index.js+31 added-60 removed
190 unmodified lines191 */192 193 Router.prototype._dispatch = function(req, res, next){194 var params = this.params195- , self = this196- , error;195+ , self = this;196 197 // route dispatch199- (function pass(i){198+ (function pass(i, err){199 var paramCallbacks200 , paramIndex = 0201 , paramVal202 , route1 unmodified line204 , key205 , ret;206 207 // match next route209- function nextRoute() {210- pass(req._route_index + 1);208+ function nextRoute(err) {209+ pass(req._route_index + 1, err);210 }211 212 // match route213 req.route = route = self._match(req, i);1 unmodified line215 // implied OPTIONS216 if (!route && 'OPTIONS' == req.method) return self._options(req, res);217 218 // no route220- if (!route) return next(error);219+ if (!route) return next(err);220 221 // we have a route222 // start at param 0223 req.params = route.params;4 unmodified lines228 function param(err) {229 key = keys[i++];230 paramVal = key && req.params[key.name];231 paramCallbacks = key && params[key.name];233-232+ 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 };251 252- param();253-252+ 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 }260 261- // 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- };274-275- // invoke middleware callback276- function done() {277- var fn = route.callback;278-261+ // 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 lines367 }368 };369 370 /**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 */379 400-Router.prototype._route = function(method, path, fn){380+Router.prototype._route = function(method, path, callbacks){381 var app = this.app402- , middleware = [];382+ , callbacks = utils.flatten(toArray(arguments, 2));383 404- // 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');386 411- // 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');414-387 // 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 });392 393 // add it394 (this.routes[method] = this.routes[method] || [])3 unmodified linesmodified:lib/router/route.js+4 added-6 removed
11 unmodified lines12131415161617181920212222232426252627282931303133323336343536373850 unmodified lines11 unmodified linesmodule.exports = Route;
/** * Initialize `Route` with the given HTTP `method`, `path`, * and callback `fn` and `options`. * and an array of `callbacks` and `options`. * * Options: * * - `sensitive` enable case-sensitive routes * - `strict` enable strict matching for trailing slashes * - `middleware` array of middleware * * @param {String} method * @param {String} path * @param {Function} fn * @param {Array} callbacks * @param {Object} options. * @api private */
function Route(method, path, fn, options) {function Route(method, path, callbacks, options) { options = options || {}; this.callback = fn; this.path = path; this.method = method; this.middleware = options.middleware; this.callbacks = callbacks; this.regexp = normalize(path , this.keys = [] , options.sensitive , options.strict);50 unmodified linesmodified:test/router.test.js+1 added-2 removed
518 unmodified lines519 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 lines581 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