Accept multiple callbacks to app.use()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixes #2224
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent a01326a · Browse files · 4 files·+114 added−41 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+114 added−41 removed
- tests:+54 added−2 removed
- other:+60 added−39 removed
Provenance
- Commit
997a558- Tree
44b5414- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
a01326a
4 files changed+114 added−41 removed
modified:lib/router/index.js+32 added-20 removed
7 unmodified lines8 var mixin = require('utils-merge');9 var debug = require('debug')('express:router');10 var parseUrl = require('parseurl');11 var slice = Array.prototype.slice;12+var toString = Object.prototype.toString;13+var utils = require('../utils');14 15 /**16 * Initialize a new `Router` with the given `options`.17 *377 unmodified lines395 * to the handler function. The main effect of this feature is that mounted396 * handlers can operate without any code changes regardless of the "prefix"397 * pathname.398 *397- * @param {String|Function} route398- * @param {Function} fn399- * @return {app} for chaining399 * @api public400 */401 403-proto.use = function(path, fn){402+proto.use = function use(fn) {403+ var offset = 0;404+ var path = '/';405+ var self = this;406+407 // default path to '/'405- if (arguments.length < 2) {406- fn = path;407- path = '/';408+ if (typeof fn !== 'function') {409+ offset = 1;410+ path = fn;411 }412 410- if (typeof fn !== 'function') {411- var type = {}.toString.call(fn);412- var msg = 'Router.use() requires callback functions but got a ' + type;413- throw new Error(msg);413+ var callbacks = utils.flatten(slice.call(arguments, offset));414+415+ if (callbacks.length === 0) {416+ throw new TypeError('Router.use() requires callback function');417 }418 416- var layer = new Layer(path, {417- sensitive: this.caseSensitive,418- strict: false,419- end: false420- }, fn);419+ callbacks.forEach(function (fn) {420+ if (typeof fn !== 'function') {421+ var type = toString.call(fn);422+ var msg = 'Router.use() requires callback function but got a ' + type;423+ throw new TypeError(msg);424+ }425 422- layer.route = undefined;426+ // add the middleware427+ debug('use %s %s', path, fn.name || '<anonymous>');428 424- // add the middleware425- debug('use %s %s', path || '/', fn.name || 'anonymous');429+ var layer = new Layer(path, {430+ sensitive: self.caseSensitive,431+ strict: false,432+ end: false433+ }, fn);434 435+ layer.route = undefined;436+427- this.stack.push(layer);437+ self.stack.push(layer);438+ });439+440 return this;441 };442 443 /**105 unmodified linesmodified:lib/application.js+27 added-19 removed
145 unmodified lines146 *147 * @api public148 */149 150-app.use = function use(path, fn){150+app.use = function use(path, fn) {151 var mount_app;152+ var mount_path;153 153- // default path to '/'154- if (arguments.length < 2) {155- fn = path;156- path = '/';154+ // check for .use(path, app) or .use(app) signature155+ if (arguments.length <= 2) {156+ mount_path = typeof path === 'string'157+ ? path158+ : '/';159+ mount_app = typeof path === 'function'160+ ? path161+ : fn;162 }163 159- // express app160- if (fn.handle && fn.set) mount_app = fn;164+ // setup router165+ this.lazyrouter();166+ var router = this._router;167 162- // restore .app property on req and res163- if (mount_app) {164- debug('.use app under %s', path);168+ // express app169+ if (mount_app && mount_app.handle && mount_app.set) {170+ debug('.use app under %s', mount_path);171 mount_app.mountpath = path;166- fn = function(req, res, next) {172+ mount_app.parent = this;173+174+ // restore .app property on req and res175+ router.use(mount_path, function mounted_app(req, res, next) {176 var orig = req.app;177 mount_app.handle(req, res, function(err) {178 req.__proto__ = orig.request;179 res.__proto__ = orig.response;180 next(err);181 });173- };174- }182+ });183 176- this.lazyrouter();177- this._router.use(path, fn);178-179- // mounted an app184+ // mounted an app180- if (mount_app) {181- mount_app.parent = this;185 mount_app.emit('mount', this);186+187+ return this;188 }189 190+ // pass-through use191+ router.use.apply(router, arguments);192+193 return this;194 };195 196 /**352 unmodified linesmodified:test/app.use.js+54 added-2 removed
14 unmodified lines15 16 app.use(blog);17 })18 19- it('should reject numbers', function(){19+ it('should reject missing functions', function(){20 var app = express();21- app.use.bind(app, 3).should.throw(/Number/);21+ app.use.bind(app, 3).should.throw(/requires callback function/);22 })23 24 describe('.use(app)', function(){25 it('should mount the app', function(done){60 unmodified lines86 })87 })88 89 describe('.use(middleware)', function(){90+ it('should accept multiple arguments', function (done) {91+ var app = express();92+93+ function fn1(req, res, next) {94+ res.setHeader('x-fn-1', 'hit');95+ next();96+ }97+98+ function fn2(req, res, next) {99+ res.setHeader('x-fn-2', 'hit');100+ next();101+ }102+103+ app.use(fn1, fn2, function fn3(req, res) {104+ res.setHeader('x-fn-3', 'hit');105+ res.end();106+ });107+108+ request(app)109+ .get('/')110+ .expect('x-fn-1', 'hit')111+ .expect('x-fn-2', 'hit')112+ .expect('x-fn-3', 'hit')113+ .expect(200, done);114+ })115+116 it('should invoke middleware for all requests', function (done) {117 var app = express();118 var cb = after(3, done);119 27 unmodified lines147 .get('/foo/bar')148 .expect(200, 'saw GET /bar', done);149 })150 151+ it('should accept multiple arguments', function (done) {152+ var app = express();153+154+ function fn1(req, res, next) {155+ res.setHeader('x-fn-1', 'hit');156+ next();157+ }158+159+ function fn2(req, res, next) {160+ res.setHeader('x-fn-2', 'hit');161+ next();162+ }163+164+ app.use('/foo', fn1, fn2, function fn3(req, res) {165+ res.setHeader('x-fn-3', 'hit');166+ res.end();167+ });168+169+ request(app)170+ .get('/foo')171+ .expect('x-fn-1', 'hit')172+ .expect('x-fn-2', 'hit')173+ .expect('x-fn-3', 'hit')174+ .expect(200, done);175+ })176+177 it('should invoke middleware for all requests starting with path', function (done) {178 var app = express();179 var cb = after(3, done);180 85 unmodified linesmodified:History.md+1 added
1 unreleased2 ==========3 4+ * accept multiple callbacks to `app.use()`5 * add explicit "Rosetta Flash JSONP abuse" protection6 - previous versions are not vulnerable; this is just explicit protection7 * catch errors in multiple `req.param(name, fn)` handlers8 * deprecate `res.redirect(url, status)` -- use `res.redirect(status, url)` instead1787 unmodified lines