Skip to content
Myr

Added mount support for res.redirect(), now respects the mount-point

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 ca184e7 · Browse files · 1 file+15 added−4 removed
This commit

State

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

Diff stats

1 file+15 added−4 removed

  • other:+15 added−4 removed

1 file+15 added−4 removed

modified:lib/response.js+15 added-4 removed
15 unmodified lines
16 , utils = connect.utils17 , parseRange = require('./utils').parseRange18 , res = http.ServerResponse.prototype19 , send = connect.static.send20 , join = require('path').join21 , mime = require('mime');2223/**24 * Send a response with the given `body` and optional `headers` and `status` code.
269 unmodified lines
294 * app.get('/post/:id', function(req, res){295 * res.redirect('comments');296 * });297 *298 * Unless an absolute `url` is given, the app's mount-point299 * will be respected. For example if we redirect to `/posts`,300 * and our app is mounted at `/blog` we will redirect to `/blog/posts`.301 *302 * @param {String} url303 * @param {Number} code304 * @api public305 */306307res.redirect = function(url, status){303 var basePath = this.app.set('home') || '/'308 var app = this.app309 , base = app.set('home') || '/'310 , status = status || 302311 , body;312313 // Setup redirect map314 var map = {309 back: this.req.header('Referrer', basePath)310 , home: basePath315 back: this.req.header('Referrer', base)316 , home: base317 };318319 // Support custom redirect map320 map.__proto__ = this.app.redirects;321322 // Attempt mapped redirect317 var mapped = typeof map[url] === 'function'323 var mapped = 'function' == typeof map[url]324 ? map[url](this.req, this)325 : map[url];326327 // Perform redirect328 url = mapped || url;329330 // Respect mount-point331 if (app.route && !~url.indexOf('://')) {332 url = join(app.route, url);333 } 334335 // Support text/{plain,html} by default336 if (this.req.accepts('html')) {337 body = '<p>' + http.STATUS_CODES[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';338 this.header('Content-Type', 'text/html');
25 unmodified lines