Skip to content
Myr

response.js

This file

Path

lib/response.js

721 lines · 16.5 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 1–11why, one step back
1/**
2 * Module dependencies.3 */45var fs = require('fs')6 , http = require('http')7 , path = require('path')8 , connect = require('connect')9 , utils = connect.utils10 , normalizeType = require('./utils').normalizeType11 , normalizeTypes = require('./utils').normalizeTypes

add etag utilTJ Holowaychukbefore adoptionf2f0976

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 13–14why, one step back
13 , statusCodes = http.STATUS_CODES
14 , send = connect.static.send

fixing cookies for connect 2.3.1TJ Holowaychukbefore adoption02d4384

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

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

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

mergeTJ Holowaychukbefore adoption214f913

line 17why, one step back
17 , crc = require('crc')

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 19–44why, one step back
19 , basename = path.basename
20 , extname = path.extname21 , join = path.join;2223/**24 * Response prototype.25 */2627var res = module.exports = {28 __proto__: http.ServerResponse.prototype29};3031/**32 * Set status `code`.33 *34 * @param {Number} code35 * @return {ServerResponse}36 * @api public37 */3839res.status = function(code){40 this.statusCode = code;41 return this;42};4344/**

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

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

mergeTJ Holowaychukbefore adoption214f913

lines 82–133why, one step back
82res.send = function(body){
83 var req = this.req84 , head = 'HEAD' == req.method85 , len;8687 // allow status / body88 if (2 == arguments.length) {89 // res.send(body, status) backwards compat90 if ('number' != typeof body && 'number' == typeof arguments[1]) {91 this.statusCode = arguments[1];92 } else {93 this.statusCode = body;94 body = arguments[1];95 }96 }9798 switch (typeof body) {99 // response status100 case 'number':101 this.get('Content-Type') || this.type('txt');102 this.statusCode = body;103 body = http.STATUS_CODES[body];104 break;105 // string defaulting to html106 case 'string':107 if (!this.get('Content-Type')) {108 this.charset = this.charset || 'utf-8';109 this.type('html');110 }111 break;112 case 'boolean':113 case 'object':114 if (null == body) {115 body = '';116 } else if (Buffer.isBuffer(body)) {117 this.get('Content-Type') || this.type('bin');118 } else {119 return this.json(body);120 }121 break;122 }123124 // populate Content-Length125 if (undefined !== body && !this.get('Content-Length')) {126 this.set('Content-Length', len = Buffer.isBuffer(body)127 ? body.length128 : Buffer.byteLength(body));129 }130131 // ETag support132 // TODO: W/ support133 if (len > 1024) {

add etag utilTJ Holowaychukbefore adoptionf2f0976

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

mergeTJ Holowaychukbefore adoption214f913

lines 137–152why, one step back
137 }
138139 // freshness140 if (req.fresh) this.statusCode = 304;141142 // strip irrelevant headers143 if (204 == this.statusCode || 304 == this.statusCode) {144 this.removeHeader('Content-Type');145 this.removeHeader('Content-Length');146 body = '';147 }148149 // respond150 this.end(head ? null : body);151 return this;152};

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 153–172why, one step back
153
154/**155 * Send JSON response.156 *157 * Examples:158 *159 * res.json(null);160 * res.json({ user: 'tj' });161 * res.json(500, 'oh noes!');162 * res.json(404, 'I dont have that');163 *164 * @param {Mixed} obj or status165 * @param {Mixed} obj166 * @return {ServerResponse}167 * @api public168 */169170res.json = function(obj){171 // allow status / body172 if (2 == arguments.length) {

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 180–181why, one step back
180 }
181

docsTJ Holowaychukbefore adoption2e8d44b

line 182why, one step back
182 // settings

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

line 187why, one step back
187

docsTJ Holowaychukbefore adoption2e8d44b

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

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

refactorTJ Holowaychukbefore adoption88072a5

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

add "jsonp callback name" settingriadhbefore adoption5426eb0

line 191why, one step back
191

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

lines 192–233why, one step back
192 return this.send(body);
193};194195/**196 * Send JSON response with JSONP callback support.197 *198 * Examples:199 *200 * res.jsonp(null);201 * res.jsonp({ user: 'tj' });202 * res.jsonp(500, 'oh noes!');203 * res.jsonp(404, 'I dont have that');204 *205 * @param {Mixed} obj or status206 * @param {Mixed} obj207 * @return {ServerResponse}208 * @api public209 */210211res.jsonp = function(obj){212 // allow status / body213 if (2 == arguments.length) {214 // res.json(body, status) backwards compat215 if ('number' == typeof arguments[1]) {216 this.statusCode = arguments[1];217 } else {218 this.statusCode = obj;219 obj = arguments[1];220 }221 }222223 // settings224 var app = this.app;225 var replacer = app.get('json replacer');226 var spaces = app.get('json spaces');227 var body = JSON.stringify(obj, replacer, spaces);228 var callback = this.req.query[app.get('jsonp callback name')];229230 // content-type231 this.charset = this.charset || 'utf-8';232 this.set('Content-Type', 'application/json');233

docsTJ Holowaychukbefore adoption2e8d44b

line 234why, one step back
234 // jsonp

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

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

fix jsonp callback char restrictionsTJ Holowaychukbefore adoption89c5aff

line 237why, one step back
237 body = callback.replace(/[^\[\]\w$.]/g, '') + '(' + body + ');';

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 238–286why, one step back
238 }
239240 return this.send(body);241};242243/**244 * Transfer the file at the given `path`.245 * 246 * Automatically sets the _Content-Type_ response header field.247 * The callback `fn(err)` is invoked when the transfer is complete248 * or when an error occurs. Be sure to check `res.sentHeader`249 * if you wish to attempt responding, as the header and some data250 * may have already been transferred.251 *252 * Options:253 *254 * - `maxAge` defaulting to 0255 * - `root` root directory for relative filenames256 *257 * Examples:258 *259 * The following example illustrates how `res.sendfile()` may260 * be used as an alternative for the `static()` middleware for261 * dynamic situations. The code backing `res.sendfile()` is actually262 * the same code, so HTTP cache support etc is identical.263 *264 * app.get('/user/:uid/photos/:file', function(req, res){265 * var uid = req.params.uid266 * , file = req.params.file;267 * 268 * req.user.mayViewFilesFrom(uid, function(yes){269 * if (yes) {270 * res.sendfile('/uploads/' + uid + '/' + file);271 * } else {272 * res.send(403, 'Sorry! you cant see that.');273 * }274 * });275 * });276 *277 * @param {String} path278 * @param {Object|Function} options or fn279 * @param {Function} fn280 * @api public281 */282283res.sendfile = function(path, options, fn){284 var self = this285 , req = self.req286 , next = this.req.next

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

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

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

line 298why, one step back
298

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

line 303why, one step back
303

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

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

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

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

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

line 307why, one step back
307

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

line 310why, one step back
310

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

line 313why, one step back
313

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

line 317why, one step back
317

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

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

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

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

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

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

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

line 330why, one step back
330 // transfer

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

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

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

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

Added res.download() testsTJ Holowaychukbefore adoption5840b42

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 350–363why, one step back
350 *
351 * @param {String} path352 * @param {String|Function} filename or fn353 * @param {Function} fn354 * @api public355 */356357res.download = function(path, filename, fn){358 // support function as second arg359 if ('function' == typeof filename) {360 fn = filename;361 filename = null;362 }363

Added res.download() testsTJ Holowaychukbefore adoption5840b42

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 367–438why, one step back
367};
368369/**370 * Set _Content-Type_ response header with `type` through `mime.lookup()`371 * when it does not contain "/", or set the Content-Type to `type` otherwise.372 *373 * Examples:374 *375 * res.type('.html');376 * res.type('html');377 * res.type('json');378 * res.type('application/json');379 * res.type('png');380 *381 * @param {String} type382 * @return {ServerResponse} for chaining383 * @api public384 */385386res.contentType =387res.type = function(type){388 return this.set('Content-Type', ~type.indexOf('/')389 ? type390 : mime.lookup(type));391};392393/**394 * Respond to the Acceptable formats using an `obj`395 * of mime-type callbacks.396 *397 * This method uses `req.accepted`, an array of398 * acceptable types ordered by their quality values.399 * When "Accept" is not present the _first_ callback400 * is invoked, otherwise the first match is used. When401 * no match is performed the server responds with402 * 406 "Not Acceptable".403 *404 * Content-Type is set for you, however if you choose405 * you may alter this within the callback using `res.type()`406 * or `res.set('Content-Type', ...)`.407 *408 * res.format({409 * 'text/plain': function(){410 * res.send('hey');411 * },412 * 413 * 'text/html': function(){414 * res.send('<p>hey</p>');415 * },416 * 417 * 'appliation/json': function(){418 * res.send({ message: 'hey' });419 * }420 * });421 *422 * In addition to canonicalized MIME types you may423 * also use extnames mapped to these types:424 *425 * res.format({426 * text: function(){427 * res.send('hey');428 * },429 * 430 * html: function(){431 * res.send('<p>hey</p>');432 * },433 * 434 * json: function(){435 * res.send({ message: 'hey' });436 * }437 * });438 *

mention res.format() callbackTJ Holowaychukbefore adoptionfd42b5c

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

docsTJ Holowaychukbefore adoptiond2baf11

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

mention res.format() callbackTJ Holowaychukbefore adoptionfd42b5c

line 444why, one step back
444 *

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 445–449why, one step back
445 * @param {Object} obj
446 * @return {ServerResponse} for chaining447 * @api public448 */449

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

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

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

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

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

line 453why, one step back
453

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

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

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

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

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

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

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 467–494why, one step back
467 } else {
468 var err = new Error('Not Acceptable');469 err.status = 406;470 err.types = normalizeTypes(keys);471 next(err);472 }473474 return this;475};476477/**478 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.479 *480 * @param {String} filename481 * @return {ServerResponse}482 * @api public483 */484485res.attachment = function(filename){486 if (filename) this.type(extname(filename));487 this.set('Content-Disposition', filename488 ? 'attachment; filename="' + basename(filename) + '"'489 : 'attachment');490 return this;491};492493/**494 * Set header `field` to `val`, or pass

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 496–512why, one step back
496 *
497 * Examples:498 *499 * res.set('Accept', 'application/json');500 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });501 *502 * Aliased as `res.header()`. 503 *504 * @param {String|Object} field505 * @param {String} val506 * @return {ServerResponse} for chaining507 * @api public508 */509510res.set = 511res.header = function(field, val){512 if (2 == arguments.length) {

Added: coerce `res.set()` values to stringsTJ Holowaychukbefore adoption67ddb42

line 513why, one step back
513 this.setHeader(field, '' + val);

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 514–515why, one step back
514 } else {
515 for (var key in field) {

Added: coerce `res.set()` values to stringsTJ Holowaychukbefore adoption67ddb42

line 516why, one step back
516 this.setHeader(key, '' + field[key]);

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 517–555why, one step back
517 }
518 }519 return this;520};521522/**523 * Get value for header `field`.524 *525 * @param {String} field526 * @return {String}527 * @api public528 */529530res.get = function(field){531 return this.getHeader(field);532};533534/**535 * Clear cookie `name`.536 *537 * @param {String} name538 * @param {Object} options539 * @param {ServerResponse} for chaining540 * @api public541 */542543res.clearCookie = function(name, options){544 var opts = { expires: new Date(1), path: '/' };545 return this.cookie(name, '', options546 ? utils.merge(opts, options)547 : opts);548};549550/**551 * Set cookie `name` to `val`, with the given `options`.552 *553 * Options:554 *555 * - `maxAge` max-age in milliseconds, converted to `expires`

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 557–574why, one step back
557 * - `path` defaults to "/"
558 *559 * Examples:560 *561 * // "Remember Me" for 15 minutes562 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });563 *564 * // save as above565 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })566 *567 * @param {String} name568 * @param {String|Object} val569 * @param {Options} options570 * @api public571 */572573res.cookie = function(name, val, options){574 options = options || {};

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

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

Merge pull request #1286 from tnydwrds/fix-signed-cookie-prefixTJ Holowaychukbefore adoption32a5c9c

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

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

coerce res.cookie values to stringsTJ Holowaychukbefore adoptionebf60d2

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

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);

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

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 }

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

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('//')) {

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

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]) {

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 649–656why, one step back
649 url = req.path + '/' + url;
650 // relative to mount-point651 } else if ('/' != url[0]) {652 url = path + '/' + url;653 }654655 // Absolute656 var host = req.get('Host');

change res.redirect() to use scheme-relative urlsTJ Holowaychukbefore adoptiond66f0e5

line 657why, one step back
657 url = '//' + host + url;

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 658–666why, one step back
658 }
659660 // Support text/{plain,html} by default661 this.format({662 text: function(){663 body = statusCodes[status] + '. Redirecting to ' + url;664 },665666 html: function(){

escape res.redirect() linkTJ Holowaychukbefore adoptionbac0c64

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

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

lines 669–672why, one step back
669 },
670671 default: function(){672 body = '';

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

line 673why, one step back
673 }

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

line 674why, one step back
674 });

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 675–678why, one step back
675
676 // Respond677 this.statusCode = status;678 this.set('Location', url);

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

lines 680–709why, one step back
680 this.end(head ? null : body);
681};682683/**684 * Render `view` with the given `options` and optional callback `fn`.685 * When a callback function is given a response will _not_ be made686 * automatically, otherwise a response of _200_ and _text/html_ is given.687 *688 * Options:689 *690 * - `cache` boolean hinting to the engine it should cache691 * - `filename` filename of the view being rendered692 *693 * @param {String} view694 * @param {Object|Function} options or callback function695 * @param {Function} fn696 * @api public697 */698699res.render = function(view, options, fn){700 var self = this701 , options = options || {}702 , req = this.req703 , app = req.app;704705 // support callback function as second arg706 if ('function' == typeof options) {707 fn = options, options = {};708 }709

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

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

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

line 712why, one step back
712

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

lines 713–717why, one step back
713 // default callback to respond
714 fn = fn || function(err, str){715 if (err) return req.next(err);716 self.send(str);717 };

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

line 718why, one step back
718

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

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

`make test` now runs unit / acceptance testsTJ Holowaychukbefore adoptionbabeb46

line 721why, one step back
721};