Skip to content
Myr

Fixed "home" setting

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Now when you use() an app, both the mounted route and previous home setting are respected. For example if you have an app mounted to /blog, and "home" for blog is /posts, home will become "/blog/posts"

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent b1a3b07 · Browse files · 2 files+33 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+33 added−7 removed

  • tests:+3 added−1 removed
  • other:+30 added−6 removed

2 files+33 added−7 removed

modified:lib/express/server.js+30 added-6 removed
31 unmodified lines
32 this.viewHelpers = {};33 this.dynamicViewHelpers = {};34 connect.Server.call(this, middleware || []);3536 // Default "home" to / 37 this.set('home', '/');3839 // Set "env" to {EXPRESS,CONNECT}_ENV40 this.set('env', process.env.EXPRESS_ENV || process.connectEnv.name);4142 // Expose objects to each other
36 unmodified lines
79 * @api public80 */8182Server.prototype.listen = function(port, host){80 // Default "home" to the mounted route or '/'81 if (!this.set('home')) {82 this.set('home', this.route || '/');83 }8483 // Setup view reloading84 if (this.set('reload views')) {85 view.watcher.call(this, this.set('reload views'));86 }8987 connect.Server.prototype.listen.call(this, port, host);88};8990/**91 * Proxy `connect.Server#use()` to apply settings to92 * mounted applications.93 *94 * @param {String|Function|Server} route95 * @param {Function|Server} middleware96 * @return {Server} for chaining97 * @api public98 */99100Server.prototype.use = function(route, middleware){101 if (typeof route !== 'string') {102 middleware = route, route = '/';103 }104105 connect.Server.prototype.use.call(this, route, middleware);106107 if (middleware instanceof Server) {108 var app = middleware,109 home = app.set('home');110 if (home === '/') home = '';111 app.set('home', (app.route || '') + home);112 }113114 return this;115};116117/**118 * Register the given view helpers `obj`. This method119 * can be called several times to apply additional helpers.120 *121 * @param {Object} obj
125 unmodified lines
modified:test/express.test.js+3 added-1 removed
267 unmodified lines
268 'test mounting': function(assert){269 var app = express.createServer(),270 blog = express.createServer(),271 map = express.createServer();272273 map.set('home', '/map');274 275 app.use('/blog', blog);276 app.use('/contact', map);277 assert.equal('/blog', blog.route);278 assert.equal('/contact', map.route);279 280 app.get('/', function(req, res){279 assert.equal('/', app.set('home'), "home did not default /");281 assert.equal('/', app.set('home'), "home did not default to /");282 assert.equal('/blog', blog.set('home'), "home did not default to Server#route when mounted");283 assert.equal('/contact/map', map.set('home'), 'home did not prepend route on Server#use()');284 res.send('main app');285 });
23 unmodified lines