Skip to content
Myr

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

4 files+34 added−23 removed

modified:lib/http.js+4 added-3 removed
232 unmodified lines
233234 // 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 lines
modified:lib/response.js+6 added-7 removed
314 unmodified lines
315 *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 minutes
10 unmodified lines
334335res.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 lines
347 *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 lines
387388res.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;394395 // Setup redirect map
15 unmodified lines
411412 // 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;416417 // Absolute418 var host = req.headers.host419 , tls = req.connection.encrypted;
17 unmodified lines
modified:test/express.test.js+16 added-5 removed
294 unmodified lines
295 , blog = express.createServer()296 , map = express.createServer()297 , reg = connect.createServer();298299 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 lines
314 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 });322323 blog.get('/', function(req, res){
44 unmodified lines
368 { url: '/' },369 { body: 'restored' }370 );371 },372373 '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 },386387 'test routes with same callback': function(){388 function handle(req, res) {389 res.send('got ' + req.string);390 }
34 unmodified lines
modified:test/response.test.js+8 added-8 removed
319 unmodified lines
320 '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 lines
418 , 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 lines
655 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 lines
678 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 lines
712 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 lines
730 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