Support mounted app as any argument to app.use()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixes #2277
authored by Fabien Franzen · committed by Douglas Christopher Wilson · parent 8449f23 · Browse files · 3 files·+71 added−28 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+71 added−28 removed
- tests:+39 added−1 removed
- other:+32 added−27 removed
Provenance
- Commit
17cea29- Tree
4f69b38- Author
Fabien Franzen <info@atelierfabien.be>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
8449f23
3 files changed+71 added−28 removed
modified:lib/application.js+31 added-27 removed
1 unmodified line2 * Module dependencies.3 */4 5 var finalhandler = require('finalhandler');6+var flatten = require('./utils').flatten;7 var mixin = require('utils-merge');8 var Router = require('./router');9 var methods = require('methods');10 var middleware = require('./middleware/init');5 unmodified lines16 var compileQueryParser = require('./utils').compileQueryParser;17 var compileTrust = require('./utils').compileTrust;18 var deprecate = require('depd')('express');19 var resolve = require('path').resolve;20+var slice = Array.prototype.slice;21 22 /**23 * Application prototype.24 */125 unmodified lines150 *151 * @api public152 */153 152-app.use = function use(path, fn) {153- var mount_app;154- var mount_path;154+app.use = function use(fn) {155+ var offset = 0;156+ var path = '/';157+ var self = this;158 156- // check for .use(path, app) or .use(app) signature157- if (arguments.length <= 2) {158- mount_path = typeof path === 'string'159- ? path160- : '/';161- mount_app = typeof path === 'function'162- ? path163- : fn;159+ // default path to '/'160+ if (typeof fn !== 'function') {161+ offset = 1;162+ path = fn;163 }164 165+ var fns = flatten(slice.call(arguments, offset));166+167+ if (fns.length === 0) {168+ throw new TypeError('app.use() requires middleware functions');169+ }170+171 // setup router172 this.lazyrouter();173 var router = this._router;174 170- // express app171- if (mount_app && mount_app.handle && mount_app.set) {172- debug('.use app under %s', mount_path);173- mount_app.mountpath = mount_path;174- mount_app.parent = this;175+ fns.forEach(function (fn) {176+ // non-express app177+ if (!fn || !fn.handle || !fn.set) {178+ return router.use(path, fn);179+ }180 181+ debug('.use app under %s', path);182+ fn.mountpath = path;183+ fn.parent = self;184+185 // restore .app property on req and res177- router.use(mount_path, function mounted_app(req, res, next) {186+ router.use(path, function mounted_app(req, res, next) {187 var orig = req.app;179- mount_app.handle(req, res, function(err) {188+ fn.handle(req, res, function (err) {189 req.__proto__ = orig.request;190 res.__proto__ = orig.response;191 next(err);192 });193 });194 195 // mounted an app187- mount_app.emit('mount', this);196+ fn.emit('mount', self);197+ });198 189- return this;190- }191-192- // pass-through use193- router.use.apply(router, arguments);194-199 return this;200 };201 202 /**214 unmodified lines417 418 this.lazyrouter();419 420 var route = this._router.route(path);417- route[method].apply(route, [].slice.call(arguments, 1));421+ route[method].apply(route, slice.call(arguments, 1));422 return this;423 };424 });425 10 unmodified lines436 app.all = function(path){437 this.lazyrouter();438 439 var route = this._router.route(path);436- var args = [].slice.call(arguments, 1);440+ var args = slice.call(arguments, 1);441 methods.forEach(function(method){442 route[method].apply(route, args);443 });444 114 unmodified linesmodified:test/app.use.js+39 added-1 removed
16 unmodified lines17 })18 19 it('should reject missing functions', function(){20 var app = express();21- app.use.bind(app, 3).should.throw(/requires callback function/);21+ app.use.bind(app, 3).should.throw(/requires middleware function/);22 })23 24 describe('.use(app)', function(){25 it('should mount the app', function(done){57 unmodified lines83 request(app)84 .get('/post/once-upon-a-time')85 .expect('success', done);86 })87+88+ it('should support mounted app anywhere', function(done){89+ var cb = after(3, done);90+ var blog = express()91+ , other = express()92+ , app = express();93+94+ function fn1(req, res, next) {95+ res.setHeader('x-fn-1', 'hit');96+ next();97+ }98+99+ function fn2(req, res, next) {100+ res.setHeader('x-fn-2', 'hit');101+ next();102+ }103+104+ blog.get('/', function(req, res){105+ res.end('success');106+ });107+108+ blog.once('mount', function (parent) {109+ parent.should.equal(app);110+ cb();111+ });112+ other.once('mount', function (parent) {113+ parent.should.equal(app);114+ cb();115+ });116+117+ app.use('/post/:article', fn1, other, fn2, blog);118+119+ request(app)120+ .get('/post/once-upon-a-time')121+ .expect('x-fn-1', 'hit')122+ .expect('x-fn-2', 'hit')123+ .expect('success', cb);124+ })125 })126 127 describe('.use(middleware)', function(){128 it('should accept multiple arguments', function (done) {175 unmodified linesmodified:History.md+1 added
3 unmodified lines4 * add `res.sendFile`5 - accepts a file system path instead of a URL6 - requires an absolute path or `root` option specified7 * deprecate `res.sendfile` -- use `res.sendFile` instead8+ * support mounted app as any argument to `app.use()`9 * deps: qs@1.0.210 - Complete rewrite11 - Limits array length to 2012 - Limits object depth to 51974 unmodified lines