Merged basepath setting. Closes #813
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent cc21f38 · Browse files · 4 files·+34 added−23 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+34 added−23 removed
- tests:+24 added−13 removed
- other:+10 added−10 removed
Provenance
- Commit
e9abe1b- Tree
51da0ff- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
cc21f38
4 files changed+34 added−23 removed
modified:lib/http.js+4 added-3 removed
232 unmodified lines233 234 // mounted an app, invoke the hook235 // and adjust some settings236 if (app) {237- home = app.set('home');238- if ('/' == home) home = '';239- app.set('home', app.route + home);237+ base = this.set('basepath') || this.route;238+ if ('/' == base) base = '';239+ base = base + (app.set('basepath') || app.route);240+ app.set('basepath', base);241 app.parent = this;242 if (app.__mounted) app.__mounted.call(app, this);243 }244 329 unmodified linesmodified:lib/response.js+6 added-7 removed
314 unmodified lines315 *316 * Options:317 *318 * - `maxAge` max-age in milliseconds, converted to `expires`319- * - `path` defaults to the "home" setting which is typically "/"319+ * - `path` defaults to the "basepath" setting which is typically "/"320 *321 * Examples:322 *323 * // "Remember Me" for 15 minutes10 unmodified lines334 335 res.cookie = function(name, val, options){336 options = options || {};337 if ('maxAge' in options) options.expires = new Date(Date.now() + options.maxAge);338- if (undefined === options.path) options.path = this.app.set('home');338+ if (undefined === options.path) options.path = this.app.set('basepath');339 var cookie = utils.serializeCookie(name, val, options);340 this.header('Set-Cookie', cookie);341 return this;342 };4 unmodified lines347 *348 * The given `url` can also be the name of a mapped url, for349 * example by default express supports "back" which redirects350 * to the _Referrer_ or _Referer_ headers or the application's351- * "home" setting. Express also supports "home" out of the box,352- * which can be set via `app.set('home', '/blog');`, and defaults353- * to '/'.351+ * "basepath" setting. Express also supports "basepath" out of the box,352+ * which can be set via `app.set('basepath', '/blog');`.353 *354 * Redirect Mapping:355 * 356 * To extend the redirect mapping capabilities that Express provides,30 unmodified lines387 388 res.redirect = function(url, status){389 var app = this.app390 , req = this.req392- , base = app.set('home') || '/'391+ , base = app.set('basepath') || app.route392 , status = status || 302393 , body;394 395 // Setup redirect map15 unmodified lines411 412 // Relative413 if (!~url.indexOf('://')) {414 // Respect mount-point416- if ('/' != app.route) url = app.route + url;415+ if ('/' != base && 0 != url.indexOf(base)) url = base + url;416 417 // Absolute418 var host = req.headers.host419 , tls = req.connection.encrypted;17 unmodified linesmodified:test/express.test.js+16 added-5 removed
294 unmodified lines295 , blog = express.createServer()296 , map = express.createServer()297 , reg = connect.createServer();298 299- map.set('home', '/map');300- 299 map.mounted(function(parent){300 called = true;301 assert.equal(this, map, 'mounted() is not in context of the child app');302 assert.equal(app, parent, 'mounted() was not called with parent app');11 unmodified lines314 app.set("test", "parent setting");315 blog.set('test').should.equal('parent setting');316 317 app.get('/', function(req, res){320- app.set('home').should.equal('/');321- blog.set('home').should.equal('/blog');322- map.set('home').should.equal('/contact/map');318+ blog.set('basepath').should.equal('/blog');319+ map.set('basepath').should.equal('/contact');320 res.send('main app');321 });322 323 blog.get('/', function(req, res){44 unmodified lines368 { url: '/' },369 { body: 'restored' }370 );371 },372+373+ 'test "basepath" setting': function(){374+ var app = express.createServer();375 376+ app.set('basepath', '/shop');377+ 378+ app.get('/redirect', function(req, res){379+ res.redirect('/cart');380+ });381+ 382+ assert.response(app,383+ { url: '/redirect', headers: { Host: 'foo.com' }},384+ { headers: { Location: 'http://foo.com/shop/cart' }});385+ },386+387 'test routes with same callback': function(){388 function handle(req, res) {389 res.send('got ' + req.string);390 }34 unmodified linesmodified:test/response.test.js+8 added-8 removed
319 unmodified lines320 'test #redirect()': function(){321 var app = express.createServer()322 , app2 = express.createServer();323 324- app2.set('home', '/blog');324+ app2.set('basepath', '/blog');325 326 app2.redirect('google', 'http://google.com');327 328 app2.redirect('blog', function(req, res){329 return req.params.id330- ? '/user/' + req.params.id + '/blog'330+ ? '/user/' + req.params.id + '/posts'331 : null;332 });333 334 app.get('/', function(req, res){83 unmodified lines418 , status: 302, headers: { Location: 'http://google.com' }});419 420 assert.response(app2,421 { url: '/user/12', headers: { Accept: 'text/plain', Host: 'foo.com' }},422- { body: 'Moved Temporarily. Redirecting to http://foo.com/user/12/blog'423- , status: 302, headers: { Location: 'http://foo.com/user/12/blog', 'X-Foo': 'bar' }});422+ { body: 'Moved Temporarily. Redirecting to http://foo.com/blog/user/12/posts'423+ , status: 302, headers: { Location: 'http://foo.com/blog/user/12/posts', 'X-Foo': 'bar' }});424 },425 426 'test #redirect() when mounted': function(){427 var app = express.createServer()227 unmodified lines655 656 'test #cookie() path default': function(){657 var app = express.createServer();658 659- app.set('home', '/foo');659+ app.set('basepath', '/foo');660 661 app.get('/', function(req, res){662 res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });663 res.cookie('something', 'else');14 unmodified lines678 app.set('/home', '/foo');679 680 app.get('/', function(req, res){681 res.cookie('rememberme', 'yes', { path: '/', expires: new Date(1), httpOnly: true });682- res.cookie('something', 'else');682+ res.cookie('something', 'else', { path: '/' });683 res.redirect('/');684 });685 686 assert.response(app,25 unmodified lines712 713 'test #clearCookie() default path': function(){714 var app = express.createServer();715 716- app.set('home', '/foo');716+ app.set('basepath', '/foo');717 718 app.get('/', function(req, res){719 res.clearCookie('rememberme');720 res.redirect('/');9 unmodified lines730 731 'test #clearCookie() explicit path': function(){732 var app = express.createServer();733 734- app.set('home', '/bar');734+ app.set('basepath', '/bar');735 736 app.get('/', function(req, res){737 res.clearCookie('rememberme', { path: '/foo' });738 res.redirect('/');35 unmodified lines