support arrays as middleware arguments to .VERB and .all
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Express 3.x supported passing in arrays for sets of common middleware. While there are better ways to do this, removing this feature causes headache in upgrading for no real gain. We can support it without much more code.
authored by Roman Shtylman · committed by Roman Shtylman · parent 6b19e3d · Browse files · 3 files·+50 added−43 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+50 added−43 removed
- other:+50 added−43 removed
Provenance
- Commit
1eba854- Tree
94a7d0f- Author
Roman Shtylman <shtylman@gmail.com>- Committer
Roman Shtylman <shtylman@gmail.com>- Parent
6b19e3d
3 files changed+50 added−43 removed
modified:lib/router/index.js+3 added-2 removed
373 unmodified lines374 };375 376 // create Router#VERB functions377 methods.concat('all').forEach(function(method){378- proto[method] = function(path, fn){379- this.route(path)[method](fn);378+ proto[method] = function(path){379+ var route = this.route(path)380+ route[method].apply(route, [].slice.call(arguments, 1));381 return this;382 };383 });384 modified:lib/router/route.js+44 added-34 removed
3 unmodified lines456789101112113 unmodified linesExpand all1261271281291291301311321331341351361371381391401411421431441451461471481491501301311321331341351531361371381391571581591601401411421621631641431441451661671461471481491501701711511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821833 unmodified lines */
var debug = require('debug')('express:router:route') , methods = require('methods') , utils = require('../utils')
/** * Expose `Route`. */113 unmodified linesExpand all * @return {Route} for chaining * @api public */
Route.prototype.all = function(fn){ if (typeof fn !== 'function') { var type = {}.toString.call(fn); var msg = 'Route.all() requires callback functions but got a ' + type; throw new Error(msg); }
if (!this.stack) { this.stack = fn; } else if (typeof this.stack === 'function') { this.stack = [{ handle: this.stack }, { handle: fn }]; } else { this.stack.push({ handle: fn }); }
return this;};
methods.forEach(function(method){ Route.prototype[method] = function(fn){Route.prototype.all = function(){ var self = this; var callbacks = utils.flatten([].slice.call(arguments)); callbacks.forEach(function(fn) { if (typeof fn !== 'function') { var type = {}.toString.call(fn); var msg = 'Route.' + method + '() requires callback functions but got a ' + type; var msg = 'Route.all() requires callback functions but got a ' + type; throw new Error(msg); }
debug('%s %s', method, this.path);
if (!this.methods[method]) { this.methods[method] = true; if (!self.stack) { self.stack = fn; }
if (!this.stack) { this.stack = []; else if (typeof self.stack === 'function') { self.stack = [{ handle: self.stack }, { handle: fn }]; } else if (typeof this.stack === 'function') { this.stack = [{ handle: this.stack }]; else { self.stack.push({ handle: fn }); } });
this.stack.push({ method: method, handle: fn }) return this; return self;};
methods.forEach(function(method){ Route.prototype[method] = function(){ var self = this; var callbacks = utils.flatten([].slice.call(arguments));
callbacks.forEach(function(fn) { if (typeof fn !== 'function') { var type = {}.toString.call(fn); var msg = 'Route.' + method + '() requires callback functions but got a ' + type; throw new Error(msg); }
debug('%s %s', method, self.path);
if (!self.methods[method]) { self.methods[method] = true; }
if (!self.stack) { self.stack = []; } else if (typeof self.stack === 'function') { self.stack = [{ handle: self.stack }]; }
self.stack.push({ method: method, handle: fn }) }); return self; };});modified:lib/application.js+3 added-7 removed
401 unmodified lines402 403 this.lazyrouter();404 405 var route = this._router.route(path);406- for (var i=1 ; i<arguments.length ; ++i) {407- route[method](arguments[i]);408- }406+ route[method].apply(route, [].slice.call(arguments, 1));407 return this;408 };409 });410 10 unmodified lines421 app.all = function(path){422 this.lazyrouter();423 424 var route = this._router.route(path);427- var args = arguments;425+ var args = [].slice.call(arguments, 1);426 methods.forEach(function(method){429- for (var i=1 ; i<args.length ; ++i) {430- route[method](args[i]);431- }427+ route[method].apply(route, args);428 });429 430 return this;431 };101 unmodified lines