Skip to content
Myr

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

4 files+114 added−41 removed

modified:lib/router/index.js+32 added-20 removed
7 unmodified lines
8var mixin = require('utils-merge');9var debug = require('debug')('express:router');10var parseUrl = require('parseurl');11var slice = Array.prototype.slice;12var toString = Object.prototype.toString;13var utils = require('../utils');1415/**16 * Initialize a new `Router` with the given `options`.17 *
377 unmodified lines
395 * 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 */401403proto.use = function(path, fn){402proto.use = function use(fn) {403 var offset = 0;404 var path = '/';405 var self = this;406407 // 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 }412410 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));414415 if (callbacks.length === 0) {416 throw new TypeError('Router.use() requires callback function');417 }418416 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 }425422 layer.route = undefined;426 // add the middleware427 debug('use %s %s', path, fn.name || '<anonymous>');428424 // 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);434435 layer.route = undefined;436427 this.stack.push(layer);437 self.stack.push(layer);438 });439440 return this;441};442443/**
105 unmodified lines
modified:lib/application.js+27 added-19 removed
145 unmodified lines
146 *147 * @api public148 */149150app.use = function use(path, fn){150app.use = function use(path, fn) {151 var mount_app;152 var mount_path;153153 // 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 }163159 // express app160 if (fn.handle && fn.set) mount_app = fn;164 // setup router165 this.lazyrouter();166 var router = this._router;167162 // 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;173174 // 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 });183176 this.lazyrouter();177 this._router.use(path, fn);178179 // mounted an app184 // mounted an app180 if (mount_app) {181 mount_app.parent = this;185 mount_app.emit('mount', this);186187 return this;188 }189190 // pass-through use191 router.use.apply(router, arguments);192193 return this;194};195196/**
352 unmodified lines
modified:test/app.use.js+54 added-2 removed
14 unmodified lines
1516 app.use(blog);17 })1819 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 })2324 describe('.use(app)', function(){25 it('should mount the app', function(done){
60 unmodified lines
86 })87 })8889 describe('.use(middleware)', function(){90 it('should accept multiple arguments', function (done) {91 var app = express();9293 function fn1(req, res, next) {94 res.setHeader('x-fn-1', 'hit');95 next();96 }9798 function fn2(req, res, next) {99 res.setHeader('x-fn-2', 'hit');100 next();101 }102103 app.use(fn1, fn2, function fn3(req, res) {104 res.setHeader('x-fn-3', 'hit');105 res.end();106 });107108 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 })115116 it('should invoke middleware for all requests', function (done) {117 var app = express();118 var cb = after(3, done);119
27 unmodified lines
147 .get('/foo/bar')148 .expect(200, 'saw GET /bar', done);149 })150151 it('should accept multiple arguments', function (done) {152 var app = express();153154 function fn1(req, res, next) {155 res.setHeader('x-fn-1', 'hit');156 next();157 }158159 function fn2(req, res, next) {160 res.setHeader('x-fn-2', 'hit');161 next();162 }163164 app.use('/foo', fn1, fn2, function fn3(req, res) {165 res.setHeader('x-fn-3', 'hit');166 res.end();167 });168169 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 })176177 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 lines
modified:History.md+1 added
1unreleased2==========34 * 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)` instead
1787 unmodified lines