Skip to content
Myr

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

3 files+71 added−28 removed

modified:lib/application.js+31 added-27 removed
1 unmodified line
2 * Module dependencies.3 */45var finalhandler = require('finalhandler');6var flatten = require('./utils').flatten;7var mixin = require('utils-merge');8var Router = require('./router');9var methods = require('methods');10var middleware = require('./middleware/init');
5 unmodified lines
16var compileQueryParser = require('./utils').compileQueryParser;17var compileTrust = require('./utils').compileTrust;18var deprecate = require('depd')('express');19var resolve = require('path').resolve;20var slice = Array.prototype.slice;2122/**23 * Application prototype.24 */
125 unmodified lines
150 *151 * @api public152 */153152app.use = function use(path, fn) {153 var mount_app;154 var mount_path;154app.use = function use(fn) {155 var offset = 0;156 var path = '/';157 var self = this;158156 // 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 }164165 var fns = flatten(slice.call(arguments, offset));166167 if (fns.length === 0) {168 throw new TypeError('app.use() requires middleware functions');169 }170171 // setup router172 this.lazyrouter();173 var router = this._router;174170 // 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 }180181 debug('.use app under %s', path);182 fn.mountpath = path;183 fn.parent = self;184185 // 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 });194195 // mounted an app187 mount_app.emit('mount', this);196 fn.emit('mount', self);197 });198189 return this;190 }191192 // pass-through use193 router.use.apply(router, arguments);194199 return this;200};201202/**
214 unmodified lines
417418 this.lazyrouter();419420 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 lines
436app.all = function(path){437 this.lazyrouter();438439 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 lines
modified:test/app.use.js+39 added-1 removed
16 unmodified lines
17 })1819 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 })2324 describe('.use(app)', function(){25 it('should mount the app', function(done){
57 unmodified lines
83 request(app)84 .get('/post/once-upon-a-time')85 .expect('success', done);86 })8788 it('should support mounted app anywhere', function(done){89 var cb = after(3, done);90 var blog = express()91 , other = express()92 , app = express();9394 function fn1(req, res, next) {95 res.setHeader('x-fn-1', 'hit');96 next();97 }9899 function fn2(req, res, next) {100 res.setHeader('x-fn-2', 'hit');101 next();102 }103104 blog.get('/', function(req, res){105 res.end('success');106 });107108 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 });116117 app.use('/post/:article', fn1, other, fn2, blog);118119 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 })126127 describe('.use(middleware)', function(){128 it('should accept multiple arguments', function (done) {
175 unmodified lines
modified:History.md+1 added
3 unmodified lines
4 * 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 5
1974 unmodified lines