Skip to content
Myr

response.js

This file

Path

lib/response.js

717 lines · 16.4 KiB · JavaScript

Last changed by

fix cookie max-ageTJ Holowaychukbefore adoptionab9c275

History of this file

Take it

The walk stopped after 200 commits: lines older than 4d9ad21 point at it.

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 1–4why, one step back
1/**
2 * Module dependencies.3 */4

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2d49c0d

line 5why, one step back
5var http = require('http')

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 6–8why, one step back
6 , path = require('path')
7 , connect = require('connect')8 , utils = connect.utils

update connectTJ Holowaychukbefore adoption612fc47

line 9why, one step back
9 , sign = require('cookie-signature').sign

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 10–11why, one step back
10 , normalizeType = require('./utils').normalizeType
11 , normalizeTypes = require('./utils').normalizeTypes

add etag utilTJ Holowaychukbefore adoptionf2f0976

line 12why, one step back
12 , etag = require('./utils').etag

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 13why, one step back
13 , statusCodes = http.STATUS_CODES

fixing cookies for connect 2.3.1TJ Holowaychukbefore adoption02d4384

line 14why, one step back
14 , cookie = require('cookie')

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

line 15why, one step back
15 , send = require('send')

Fixed: expose connects mime module. Cloases #1165TJ Holowaychukbefore adoption8956731

line 16why, one step back
16 , mime = connect.mime

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 17–42why, one step back
17 , basename = path.basename
18 , extname = path.extname19 , join = path.join;2021/**22 * Response prototype.23 */2425var res = module.exports = {26 __proto__: http.ServerResponse.prototype27};2829/**30 * Set status `code`.31 *32 * @param {Number} code33 * @return {ServerResponse}34 * @api public35 */3637res.status = function(code){38 this.statusCode = code;39 return this;40};4142/**

Added `res.links(obj)`TJ Holowaychukbefore adoptione6129d8

lines 43–63why, one step back
43 * Set Link header field with the given `links`.
44 *45 * Examples:46 *47 * res.links({48 * next: 'http://api.example.com/users?page=2',49 * last: 'http://api.example.com/users?page=5'50 * });51 *52 * @param {Object} links53 * @return {ServerResponse}54 * @api public55 */5657res.links = function(links){58 return this.set('Link', Object.keys(links).map(function(rel){59 return '<' + links[rel] + '>; rel="' + rel + '"';60 }).join(', '));61};6263/**

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 64–79why, one step back
64 * Send a response.
65 *66 * Examples:67 *68 * res.send(new Buffer('wahoo'));69 * res.send({ some: 'json' });70 * res.send('<p>some html</p>');71 * res.send(404, 'Sorry, cant find that');72 * res.send(404);73 *74 * @param {Mixed} body or status75 * @param {Mixed} body76 * @return {ServerResponse}77 * @api public78 */79

mergeTJ Holowaychukbefore adoption214f913

lines 80–131why, one step back
80res.send = function(body){
81 var req = this.req82 , head = 'HEAD' == req.method83 , len;8485 // allow status / body86 if (2 == arguments.length) {87 // res.send(body, status) backwards compat88 if ('number' != typeof body && 'number' == typeof arguments[1]) {89 this.statusCode = arguments[1];90 } else {91 this.statusCode = body;92 body = arguments[1];93 }94 }9596 switch (typeof body) {97 // response status98 case 'number':99 this.get('Content-Type') || this.type('txt');100 this.statusCode = body;101 body = http.STATUS_CODES[body];102 break;103 // string defaulting to html104 case 'string':105 if (!this.get('Content-Type')) {106 this.charset = this.charset || 'utf-8';107 this.type('html');108 }109 break;110 case 'boolean':111 case 'object':112 if (null == body) {113 body = '';114 } else if (Buffer.isBuffer(body)) {115 this.get('Content-Type') || this.type('bin');116 } else {117 return this.json(body);118 }119 break;120 }121122 // populate Content-Length123 if (undefined !== body && !this.get('Content-Length')) {124 this.set('Content-Length', len = Buffer.isBuffer(body)125 ? body.length126 : Buffer.byteLength(body));127 }128129 // ETag support130 // TODO: W/ support131 if (len > 1024) {

add etag utilTJ Holowaychukbefore adoptionf2f0976

lines 132–134why, one step back
132 if (!this.get('ETag')) {
133 this.set('ETag', etag(body));134 }

mergeTJ Holowaychukbefore adoption214f913

lines 135–150why, one step back
135 }
136137 // freshness138 if (req.fresh) this.statusCode = 304;139140 // strip irrelevant headers141 if (204 == this.statusCode || 304 == this.statusCode) {142 this.removeHeader('Content-Type');143 this.removeHeader('Content-Length');144 body = '';145 }146147 // respond148 this.end(head ? null : body);149 return this;150};

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 151–170why, one step back
151
152/**153 * Send JSON response.154 *155 * Examples:156 *157 * res.json(null);158 * res.json({ user: 'tj' });159 * res.json(500, 'oh noes!');160 * res.json(404, 'I dont have that');161 *162 * @param {Mixed} obj or status163 * @param {Mixed} obj164 * @return {ServerResponse}165 * @api public166 */167168res.json = function(obj){169 // allow status / body170 if (2 == arguments.length) {

Added `res.json(obj, status)` support back for BCTJ Holowaychukbefore adoption70d6841

lines 171–177why, one step back
171 // res.json(body, status) backwards compat
172 if ('number' == typeof arguments[1]) {173 this.statusCode = arguments[1];174 } else {175 this.statusCode = obj;176 obj = arguments[1];177 }

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 178–179why, one step back
178 }
179

docsTJ Holowaychukbefore adoption2e8d44b

line 180why, one step back
180 // settings

add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26

lines 181–184why, one step back
181 var app = this.app;
182 var replacer = app.get('json replacer');183 var spaces = app.get('json spaces');184 var body = JSON.stringify(obj, replacer, spaces);

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 185why, one step back
185

docsTJ Holowaychukbefore adoption2e8d44b

line 186why, one step back
186 // content-type

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 187why, one step back
187 this.charset = this.charset || 'utf-8';

refactorTJ Holowaychukbefore adoption88072a5

line 188why, one step back
188 this.get('Content-Type') || this.set('Content-Type', 'application/json');

add "jsonp callback name" settingriadhbefore adoption5426eb0

line 189why, one step back
189

add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26

lines 190–231why, one step back
190 return this.send(body);
191};192193/**194 * Send JSON response with JSONP callback support.195 *196 * Examples:197 *198 * res.jsonp(null);199 * res.jsonp({ user: 'tj' });200 * res.jsonp(500, 'oh noes!');201 * res.jsonp(404, 'I dont have that');202 *203 * @param {Mixed} obj or status204 * @param {Mixed} obj205 * @return {ServerResponse}206 * @api public207 */208209res.jsonp = function(obj){210 // allow status / body211 if (2 == arguments.length) {212 // res.json(body, status) backwards compat213 if ('number' == typeof arguments[1]) {214 this.statusCode = arguments[1];215 } else {216 this.statusCode = obj;217 obj = arguments[1];218 }219 }220221 // settings222 var app = this.app;223 var replacer = app.get('json replacer');224 var spaces = app.get('json spaces');225 var body = JSON.stringify(obj, replacer, spaces);226 var callback = this.req.query[app.get('jsonp callback name')];227228 // content-type229 this.charset = this.charset || 'utf-8';230 this.set('Content-Type', 'application/json');231

docsTJ Holowaychukbefore adoption2e8d44b

line 232why, one step back
232 // jsonp

add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26

line 233why, one step back
233 if (callback) {

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 234why, one step back
234 this.set('Content-Type', 'text/javascript');

add cb && cb(payload) to `res.jsonp()`. Closes #1374TJ Holowaychukbefore adoptionb3936b9

lines 235–236why, one step back
235 var cb = callback.replace(/[^\[\]\w$.]/g, '');
236 body = cb + ' && ' + cb + '(' + body + ');';

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 237–285why, one step back
237 }
238239 return this.send(body);240};241242/**243 * Transfer the file at the given `path`.244 * 245 * Automatically sets the _Content-Type_ response header field.246 * The callback `fn(err)` is invoked when the transfer is complete247 * or when an error occurs. Be sure to check `res.sentHeader`248 * if you wish to attempt responding, as the header and some data249 * may have already been transferred.250 *251 * Options:252 *253 * - `maxAge` defaulting to 0254 * - `root` root directory for relative filenames255 *256 * Examples:257 *258 * The following example illustrates how `res.sendfile()` may259 * be used as an alternative for the `static()` middleware for260 * dynamic situations. The code backing `res.sendfile()` is actually261 * the same code, so HTTP cache support etc is identical.262 *263 * app.get('/user/:uid/photos/:file', function(req, res){264 * var uid = req.params.uid265 * , file = req.params.file;266 * 267 * req.user.mayViewFilesFrom(uid, function(yes){268 * if (yes) {269 * res.sendfile('/uploads/' + uid + '/' + file);270 * } else {271 * res.send(403, 'Sorry! you cant see that.');272 * }273 * });274 * });275 *276 * @param {String} path277 * @param {Object|Function} options or fn278 * @param {Function} fn279 * @api public280 */281282res.sendfile = function(path, options, fn){283 var self = this284 , req = self.req285 , next = this.req.next

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 286–287why, one step back
286 , options = options || {}
287 , done;

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 288–294why, one step back
288
289 // support function as second arg290 if ('function' == typeof options) {291 fn = options;292 options = {};293 }294

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 295–296why, one step back
295 // socket errors
296 req.socket.on('error', error);

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 297why, one step back
297

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 298–301why, one step back
298 // errors
299 function error(err) {300 if (done) return;301 done = true;

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 302why, one step back
302

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

line 303why, one step back
303 // clean up

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption3865168

line 304why, one step back
304 cleanup();

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

line 305why, one step back
305 if (!self.headerSent) self.removeHeader('Content-Disposition');

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 306why, one step back
306

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 307–308why, one step back
307 // callback available
308 if (fn) return fn(err);

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 309why, one step back
309

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 310–311why, one step back
310 // list in limbo if there's no callback
311 if (self.headerSent) return;

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 312why, one step back
312

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 313–315why, one step back
313 // delegate
314 next(err);315 }

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 316why, one step back
316

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 317–319why, one step back
317 // streaming
318 function stream() {319 if (done) return;

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption3865168

line 320why, one step back
320 cleanup();

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 321–323why, one step back
321 if (fn) self.on('finish', fn);
322 }323

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption3865168

lines 324–328why, one step back
324 // cleanup
325 function cleanup() {326 req.socket.removeListener('error', error);327 }328

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 329why, one step back
329 // transfer

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 330–336why, one step back
330 var file = send(req, path);
331 if (options.root) file.root(options.root);332 file.maxage(options.maxAge || 0);333 file.on('error', error);334 file.on('directory', next);335 file.on('stream', stream);336 file.pipe(this);

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption3865168

line 337why, one step back
337 this.on('finish', cleanup);

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 338–347why, one step back
338};
339340/**341 * Transfer the file at the given `path` as an attachment.342 *343 * Optionally providing an alternate attachment `filename`,344 * and optional callback `fn(err)`. The callback is invoked345 * when the data transfer is complete, or when an error has346 * ocurred. Be sure to check `res.headerSent` if you plan to respond.347 *

Added res.download() testsTJ Holowaychukbefore adoption5840b42

line 348why, one step back
348 * This method uses `res.sendfile()`.

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 349–362why, one step back
349 *
350 * @param {String} path351 * @param {String|Function} filename or fn352 * @param {Function} fn353 * @api public354 */355356res.download = function(path, filename, fn){357 // support function as second arg358 if ('function' == typeof filename) {359 fn = filename;360 filename = null;361 }362

Added res.download() testsTJ Holowaychukbefore adoption5840b42

lines 363–365why, one step back
363 filename = filename || path;
364 this.set('Content-Disposition', 'attachment; filename="' + basename(filename) + '"');365 return this.sendfile(path, fn);

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 366–437why, one step back
366};
367368/**369 * Set _Content-Type_ response header with `type` through `mime.lookup()`370 * when it does not contain "/", or set the Content-Type to `type` otherwise.371 *372 * Examples:373 *374 * res.type('.html');375 * res.type('html');376 * res.type('json');377 * res.type('application/json');378 * res.type('png');379 *380 * @param {String} type381 * @return {ServerResponse} for chaining382 * @api public383 */384385res.contentType =386res.type = function(type){387 return this.set('Content-Type', ~type.indexOf('/')388 ? type389 : mime.lookup(type));390};391392/**393 * Respond to the Acceptable formats using an `obj`394 * of mime-type callbacks.395 *396 * This method uses `req.accepted`, an array of397 * acceptable types ordered by their quality values.398 * When "Accept" is not present the _first_ callback399 * is invoked, otherwise the first match is used. When400 * no match is performed the server responds with401 * 406 "Not Acceptable".402 *403 * Content-Type is set for you, however if you choose404 * you may alter this within the callback using `res.type()`405 * or `res.set('Content-Type', ...)`.406 *407 * res.format({408 * 'text/plain': function(){409 * res.send('hey');410 * },411 * 412 * 'text/html': function(){413 * res.send('<p>hey</p>');414 * },415 * 416 * 'appliation/json': function(){417 * res.send({ message: 'hey' });418 * }419 * });420 *421 * In addition to canonicalized MIME types you may422 * also use extnames mapped to these types:423 *424 * res.format({425 * text: function(){426 * res.send('hey');427 * },428 * 429 * html: function(){430 * res.send('<p>hey</p>');431 * },432 * 433 * json: function(){434 * res.send({ message: 'hey' });435 * }436 * });437 *

mention res.format() callbackTJ Holowaychukbefore adoptionfd42b5c

lines 438–439why, one step back
438 * By default Express passes an `Error`
439 * with a `.status` of 406 to `next(err)`

docsTJ Holowaychukbefore adoptiond2baf11

lines 440–442why, one step back
440 * if a match is not made. If you provide
441 * a `.default` callback it will be invoked442 * instead.

mention res.format() callbackTJ Holowaychukbefore adoptionfd42b5c

line 443why, one step back
443 *

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 444–448why, one step back
444 * @param {Object} obj
445 * @return {ServerResponse} for chaining446 * @api public447 */448

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

line 449why, one step back
449res.format = function(obj){

refactor res.format(), dont pass "default" to req.accepts()TJ Holowaychukbefore adoption8edf358

line 450why, one step back
450 var req = this.req

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

line 451why, one step back
451 , next = req.next;

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 452why, one step back
452

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

lines 453–454why, one step back
453 var fn = obj.default;
454 if (fn) delete obj.default;

refactor res.format(), dont pass "default" to req.accepts()TJ Holowaychukbefore adoption8edf358

line 455why, one step back
455 var keys = Object.keys(obj);

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

lines 456–458why, one step back
456
457 var key = req.accepts(keys);458

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 459–463why, one step back
459 this.set('Vary', 'Accept');
460461 if (key) {462 this.set('Content-Type', normalizeType(key));463 obj[key](req, this, next);

Fixed res.redirect() 406. Closes #1154TJ Holowaychukbefore adoption1311f2a

lines 464–465why, one step back
464 } else if (fn) {
465 fn();

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 466–493why, one step back
466 } else {
467 var err = new Error('Not Acceptable');468 err.status = 406;469 err.types = normalizeTypes(keys);470 next(err);471 }472473 return this;474};475476/**477 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.478 *479 * @param {String} filename480 * @return {ServerResponse}481 * @api public482 */483484res.attachment = function(filename){485 if (filename) this.type(extname(filename));486 this.set('Content-Disposition', filename487 ? 'attachment; filename="' + basename(filename) + '"'488 : 'attachment');489 return this;490};491492/**493 * Set header `field` to `val`, or pass

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption0acee67

line 494why, one step back
494 * an object of header fields.

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 495–554why, one step back
495 *
496 * Examples:497 *498 * res.set('Accept', 'application/json');499 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });500 *501 * Aliased as `res.header()`. 502 *503 * @param {String|Object} field504 * @param {String} val505 * @return {ServerResponse} for chaining506 * @api public507 */508509res.set = 510res.header = function(field, val){511 if (2 == arguments.length) {512 this.setHeader(field, '' + val);513 } else {514 for (var key in field) {515 this.setHeader(key, '' + field[key]);516 }517 }518 return this;519};520521/**522 * Get value for header `field`.523 *524 * @param {String} field525 * @return {String}526 * @api public527 */528529res.get = function(field){530 return this.getHeader(field);531};532533/**534 * Clear cookie `name`.535 *536 * @param {String} name537 * @param {Object} options538 * @param {ServerResponse} for chaining539 * @api public540 */541542res.clearCookie = function(name, options){543 var opts = { expires: new Date(1), path: '/' };544 return this.cookie(name, '', options545 ? utils.merge(opts, options)546 : opts);547};548549/**550 * Set cookie `name` to `val`, with the given `options`.551 *552 * Options:553 *554 * - `maxAge` max-age in milliseconds, converted to `expires`

Added { signed: true } option to res.cookie()TJ Holowaychukbefore adoption1150a88

line 555why, one step back
555 * - `signed` sign the cookie

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 556–573why, one step back
556 * - `path` defaults to "/"
557 *558 * Examples:559 *560 * // "Remember Me" for 15 minutes561 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });562 *563 * // save as above564 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })565 *566 * @param {String} name567 * @param {String|Object} val568 * @param {Options} options569 * @api public570 */571572res.cookie = function(name, val, options){573 options = options || {};

Added { signed: true } option to res.cookie()TJ Holowaychukbefore adoption1150a88

lines 574–576why, one step back
574 var secret = this.req.secret;
575 var signed = options.signed;576 if (signed && !secret) throw new Error('connect.cookieParser("secret") required for signed cookies');

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 577why, one step back
577 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);

update connectTJ Holowaychukbefore adoption612fc47

line 578why, one step back
578 if (signed) val = 's:' + sign(val, secret);

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 579–580why, one step back
579 if ('maxAge' in options) options.expires = new Date(Date.now() + options.maxAge);
580 if (null == options.path) options.path = '/';

fix cookie max-ageTJ Holowaychukbefore adoptionab9c275

line 581why, one step back
581 options.maxAge /= 1000;

coerce res.cookie values to stringsTJ Holowaychukbefore adoptionebf60d2

line 582why, one step back
582 this.set('Set-Cookie', cookie.serialize(name, String(val), options));

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 583–598why, one step back
583 return this;
584};585586/**587 * Redirect to the given `url` with optional response `status`588 * defaulting to 302.589 *590 * The given `url` can also be the name of a mapped url, for591 * example by default express supports "back" which redirects592 * to the _Referrer_ or _Referer_ headers or "/".593 *594 * Examples:595 *596 * res.redirect('/foo/bar');597 * res.redirect('http://example.com');598 * res.redirect(301, 'http://example.com');

add backwards compat for res.redirect() status. Closes #1336TJ Holowaychukbefore adoption56ffab0

line 599why, one step back
599 * res.redirect('http://example.com', 301);

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 600–628why, one step back
600 * res.redirect('../login'); // /blog/post/1 -> /blog/login
601 *602 * Mounting:603 *604 * When an application is mounted, and `res.redirect()`605 * is given a path that does _not_ lead with "/". For 606 * example suppose a "blog" app is mounted at "/blog",607 * the following redirect would result in "/blog/login":608 *609 * res.redirect('login');610 *611 * While the leading slash would result in a redirect to "/login":612 *613 * res.redirect('/login');614 *615 * @param {String} url616 * @param {Number} code617 * @api public618 */619620res.redirect = function(url){621 var app = this.app622 , req = this.req623 , head = 'HEAD' == req.method624 , status = 302625 , body;626627 // allow status / url628 if (2 == arguments.length) {

add backwards compat for res.redirect() status. Closes #1336TJ Holowaychukbefore adoption56ffab0

lines 629–634why, one step back
629 if ('number' == typeof url) {
630 status = url;631 url = arguments[1];632 } else {633 status = arguments[1];634 }

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 635–643why, one step back
635 }
636637 // setup redirect map638 var map = { back: req.get('Referrer') || '/' };639640 // perform redirect641 url = map[url] || url;642643 // relative

add res.redirect("//foo.com") supportTJ Holowaychukbefore adoptiona93d375

line 644why, one step back
644 if (!~url.indexOf('://') && 0 != url.indexOf('//')) {

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 645–647why, one step back
645 var path = app.path();
646647 // relative to path

refactor res.redirect() relative checkTJ Holowaychukbefore adoption362c96c

line 648why, one step back
648 if ('.' == url[0]) {

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 649–662why, one step back
649 url = req.path + '/' + url;
650 // relative to mount-point651 } else if ('/' != url[0]) {652 url = path + '/' + url;653 }654 }655656 // Support text/{plain,html} by default657 this.format({658 text: function(){659 body = statusCodes[status] + '. Redirecting to ' + url;660 },661662 html: function(){

escape res.redirect() linkTJ Holowaychukbefore adoptionbac0c64

lines 663–664why, one step back
663 var u = utils.escape(url);
664 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

lines 665–668why, one step back
665 },
666667 default: function(){668 body = '';

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 669why, one step back
669 }

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

line 670why, one step back
670 });

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 671–674why, one step back
671
672 // Respond673 this.statusCode = status;674 this.set('Location', url);

Fixed res.redirect() 406. Closes #1154TJ Holowaychukbefore adoption1311f2a

line 675why, one step back
675 this.set('Content-Length', Buffer.byteLength(body));

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

lines 676–705why, one step back
676 this.end(head ? null : body);
677};678679/**680 * Render `view` with the given `options` and optional callback `fn`.681 * When a callback function is given a response will _not_ be made682 * automatically, otherwise a response of _200_ and _text/html_ is given.683 *684 * Options:685 *686 * - `cache` boolean hinting to the engine it should cache687 * - `filename` filename of the view being rendered688 *689 * @param {String} view690 * @param {Object|Function} options or callback function691 * @param {Function} fn692 * @api public693 */694695res.render = function(view, options, fn){696 var self = this697 , options = options || {}698 , req = this.req699 , app = req.app;700701 // support callback function as second arg702 if ('function' == typeof options) {703 fn = options, options = {};704 }705

remove app.locals.use and res.locals.useTJ Holowaychukbefore adoption4f7c4d1

line 706why, one step back
706 // merge res.locals

refactor res.render() to prevent clobbering "locals"TJ Holowaychukbefore adoption6bebe08

line 707why, one step back
707 options._locals = self.locals;

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 708why, one step back
708

remove app.locals.use and res.locals.useTJ Holowaychukbefore adoption4f7c4d1

lines 709–713why, one step back
709 // default callback to respond
710 fn = fn || function(err, str){711 if (err) return req.next(err);712 self.send(str);713 };

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 714why, one step back
714

remove app.locals.use and res.locals.useTJ Holowaychukbefore adoption4f7c4d1

lines 715–716why, one step back
715 // render
716 app.render(view, options, fn);

refactored * routing testTJ Holowaychukbefore adoption4d9ad21

line 717why, one step back
717};