Skip to content
Myr

response.js

This file

Path

lib/response.js

799 lines · 18.3 KiB · JavaScript

Last changed by

ocdTJ Holowaychukbefore adoption8a7a695

History of this file

Take it

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

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

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

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

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

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

lines 10–16why, one step back
10 , normalizeType = require('./utils').normalizeType
11 , normalizeTypes = require('./utils').normalizeTypes12 , etag = require('./utils').etag13 , statusCodes = http.STATUS_CODES14 , cookie = require('cookie')15 , send = require('send')16 , mime = connect.mime

Merge pull request #1802 from kapouer/patch-1Jonathan Ongbefore adoption2bc703c

line 17why, one step back
17 , resolve = require('url').resolve

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

line 18why, one step back
18 , basename = path.basename

lint: remove unused stuffJonathan Ongbefore adoption9bed2b8

line 19why, one step back
19 , extname = path.extname;

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

lines 20–57why, one step back
20
21/**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/**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){

refactor res.links()TJ Holowaychukbefore adoption8e2f538

lines 58–60why, one step back
58 var link = this.get('Link') || '';
59 if (link) link += ', ';60 return this.set('Link', link + Object.keys(links).map(function(rel){

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

lines 61–82why, one step back
61 return '<' + links[rel] + '>; rel="' + rel + '"';
62 }).join(', '));63};6465/**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 */8182res.send = function(body){

change generation of ETags with res.send() to GET requests only. Closes #1619TJ Holowaychukbefore adoption3296ed9

lines 83–85why, one step back
83 var req = this.req;
84 var head = 'HEAD' == req.method;85 var len;

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

line 86why, one step back
86

Merge pull request #1694 from kavu/add_disable_etagTJ Holowaychukbefore adoption610e172

lines 87–89why, one step back
87 // settings
88 var app = this.app;89

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

lines 90–135why, one step back
90 // allow status / body
91 if (2 == arguments.length) {92 // res.send(body, status) backwards compat93 if ('number' != typeof body && 'number' == typeof arguments[1]) {94 this.statusCode = arguments[1];95 } else {96 this.statusCode = body;97 body = arguments[1];98 }99 }100101 switch (typeof body) {102 // response status103 case 'number':104 this.get('Content-Type') || this.type('txt');105 this.statusCode = body;106 body = http.STATUS_CODES[body];107 break;108 // string defaulting to html109 case 'string':110 if (!this.get('Content-Type')) {111 this.charset = this.charset || 'utf-8';112 this.type('html');113 }114 break;115 case 'boolean':116 case 'object':117 if (null == body) {118 body = '';119 } else if (Buffer.isBuffer(body)) {120 this.get('Content-Type') || this.type('bin');121 } else {122 return this.json(body);123 }124 break;125 }126127 // populate Content-Length128 if (undefined !== body && !this.get('Content-Length')) {129 this.set('Content-Length', len = Buffer.isBuffer(body)130 ? body.length131 : Buffer.byteLength(body));132 }133134 // ETag support135 // TODO: W/ support

always send ETag when content-length > 0Jonathan Ongbefore adoption55d1a4f

line 136why, one step back
136 if (app.settings.etag && len && 'GET' == req.method) {

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

lines 137–148why, one step back
137 if (!this.get('ETag')) {
138 this.set('ETag', etag(body));139 }140 }141142 // freshness143 if (req.fresh) this.statusCode = 304;144145 // strip irrelevant headers146 if (204 == this.statusCode || 304 == this.statusCode) {147 this.removeHeader('Content-Type');148 this.removeHeader('Content-Length');

Merge pull request #1451 from aweeks/fix-304-must-not-contain-bodyTJ Holowaychukbefore adoption3c4fd57

line 149why, one step back
149 this.removeHeader('Transfer-Encoding');

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

lines 150–192why, one step back
150 body = '';
151 }152153 // respond154 this.end(head ? null : body);155 return this;156};157158/**159 * Send JSON response.160 *161 * Examples:162 *163 * res.json(null);164 * res.json({ user: 'tj' });165 * res.json(500, 'oh noes!');166 * res.json(404, 'I dont have that');167 *168 * @param {Mixed} obj or status169 * @param {Mixed} obj170 * @return {ServerResponse}171 * @api public172 */173174res.json = function(obj){175 // allow status / body176 if (2 == arguments.length) {177 // res.json(body, status) backwards compat178 if ('number' == typeof arguments[1]) {179 this.statusCode = arguments[1];180 } else {181 this.statusCode = obj;182 obj = arguments[1];183 }184 }185186 // settings187 var app = this.app;188 var replacer = app.get('json replacer');189 var spaces = app.get('json spaces');190 var body = JSON.stringify(obj, replacer, spaces);191192 // content-type

Revert "remove charset from json responses. Closes #1631"TJ Holowaychukbefore adoption6cf6c8b

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

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

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

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 195why, one step back
195

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

lines 196–230why, one step back
196 return this.send(body);
197};198199/**200 * Send JSON response with JSONP callback support.201 *202 * Examples:203 *204 * res.jsonp(null);205 * res.jsonp({ user: 'tj' });206 * res.jsonp(500, 'oh noes!');207 * res.jsonp(404, 'I dont have that');208 *209 * @param {Mixed} obj or status210 * @param {Mixed} obj211 * @return {ServerResponse}212 * @api public213 */214215res.jsonp = function(obj){216 // allow status / body217 if (2 == arguments.length) {218 // res.json(body, status) backwards compat219 if ('number' == typeof arguments[1]) {220 this.statusCode = arguments[1];221 } else {222 this.statusCode = obj;223 obj = arguments[1];224 }225 }226227 // settings228 var app = this.app;229 var replacer = app.get('json replacer');230 var spaces = app.get('json spaces');

fix jsonp whitespace escape. Closes #1132Guillermo Rauchbefore adoption64a2349

lines 231–233why, one step back
231 var body = JSON.stringify(obj, replacer, spaces)
232 .replace(/\u2028/g, '\\u2028')233 .replace(/\u2029/g, '\\u2029');

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

lines 234–238why, one step back
234 var callback = this.req.query[app.get('jsonp callback name')];
235236 // content-type237 this.charset = this.charset || 'utf-8';238 this.set('Content-Type', 'application/json');

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 239why, one step back
239

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

lines 240–241why, one step back
240 // jsonp
241 if (callback) {

Merge pull request #1711 from jonjenkins/masterTJ Holowaychukbefore adoption3ea7381

line 242why, one step back
242 if (Array.isArray(callback)) callback = callback[0];

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

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

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

line 244why, one step back
244 var cb = callback.replace(/[^\[\]\w$.]/g, '');

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

line 245why, one step back
245 body = 'typeof ' + cb + ' === \'function\' && ' + cb + '(' + body + ');';

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

lines 246–252why, one step back
246 }
247248 return this.send(body);249};250251/**252 * Transfer the file at the given `path`.

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 253why, one step back
253 *

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

lines 254–274why, one step back
254 * Automatically sets the _Content-Type_ response header field.
255 * The callback `fn(err)` is invoked when the transfer is complete256 * or when an error occurs. Be sure to check `res.sentHeader`257 * if you wish to attempt responding, as the header and some data258 * may have already been transferred.259 *260 * Options:261 *262 * - `maxAge` defaulting to 0263 * - `root` root directory for relative filenames264 *265 * Examples:266 *267 * The following example illustrates how `res.sendfile()` may268 * be used as an alternative for the `static()` middleware for269 * dynamic situations. The code backing `res.sendfile()` is actually270 * the same code, so HTTP cache support etc is identical.271 *272 * app.get('/user/:uid/photos/:file', function(req, res){273 * var uid = req.params.uid274 * , file = req.params.file;

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 275why, one step back
275 *

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

lines 276–326why, one step back
276 * req.user.mayViewFilesFrom(uid, function(yes){
277 * if (yes) {278 * res.sendfile('/uploads/' + uid + '/' + file);279 * } else {280 * res.send(403, 'Sorry! you cant see that.');281 * }282 * });283 * });284 *285 * @param {String} path286 * @param {Object|Function} options or fn287 * @param {Function} fn288 * @api public289 */290291res.sendfile = function(path, options, fn){292 var self = this293 , req = self.req294 , next = this.req.next295 , options = options || {}296 , done;297298 // support function as second arg299 if ('function' == typeof options) {300 fn = options;301 options = {};302 }303304 // socket errors305 req.socket.on('error', error);306307 // errors308 function error(err) {309 if (done) return;310 done = true;311312 // clean up313 cleanup();314 if (!self.headerSent) self.removeHeader('Content-Disposition');315316 // callback available317 if (fn) return fn(err);318319 // list in limbo if there's no callback320 if (self.headerSent) return;321322 // delegate323 next(err);324 }325326 // streaming

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

line 327why, one step back
327 function stream(stream) {

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

lines 328–329why, one step back
328 if (done) return;
329 cleanup();

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

line 330why, one step back
330 if (fn) stream.on('end', fn);

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

lines 331–419why, one step back
331 }
332333 // cleanup334 function cleanup() {335 req.socket.removeListener('error', error);336 }337338 // transfer339 var file = send(req, path);340 if (options.root) file.root(options.root);341 file.maxage(options.maxAge || 0);342 file.on('error', error);343 file.on('directory', next);344 file.on('stream', stream);345 file.pipe(this);346 this.on('finish', cleanup);347};348349/**350 * Transfer the file at the given `path` as an attachment.351 *352 * Optionally providing an alternate attachment `filename`,353 * and optional callback `fn(err)`. The callback is invoked354 * when the data transfer is complete, or when an error has355 * ocurred. Be sure to check `res.headerSent` if you plan to respond.356 *357 * This method uses `res.sendfile()`.358 *359 * @param {String} path360 * @param {String|Function} filename or fn361 * @param {Function} fn362 * @api public363 */364365res.download = function(path, filename, fn){366 // support function as second arg367 if ('function' == typeof filename) {368 fn = filename;369 filename = null;370 }371372 filename = filename || path;373 this.set('Content-Disposition', 'attachment; filename="' + basename(filename) + '"');374 return this.sendfile(path, fn);375};376377/**378 * Set _Content-Type_ response header with `type` through `mime.lookup()`379 * when it does not contain "/", or set the Content-Type to `type` otherwise.380 *381 * Examples:382 *383 * res.type('.html');384 * res.type('html');385 * res.type('json');386 * res.type('application/json');387 * res.type('png');388 *389 * @param {String} type390 * @return {ServerResponse} for chaining391 * @api public392 */393394res.contentType =395res.type = function(type){396 return this.set('Content-Type', ~type.indexOf('/')397 ? type398 : mime.lookup(type));399};400401/**402 * Respond to the Acceptable formats using an `obj`403 * of mime-type callbacks.404 *405 * This method uses `req.accepted`, an array of406 * acceptable types ordered by their quality values.407 * When "Accept" is not present the _first_ callback408 * is invoked, otherwise the first match is used. When409 * no match is performed the server responds with410 * 406 "Not Acceptable".411 *412 * Content-Type is set for you, however if you choose413 * you may alter this within the callback using `res.type()`414 * or `res.set('Content-Type', ...)`.415 *416 * res.format({417 * 'text/plain': function(){418 * res.send('hey');419 * },

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 420why, one step back
420 *

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

lines 421–423why, one step back
421 * 'text/html': function(){
422 * res.send('<p>hey</p>');423 * },

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 424why, one step back
424 *

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

lines 425–436why, one step back
425 * 'appliation/json': function(){
426 * res.send({ message: 'hey' });427 * }428 * });429 *430 * In addition to canonicalized MIME types you may431 * also use extnames mapped to these types:432 *433 * res.format({434 * text: function(){435 * res.send('hey');436 * },

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 437why, one step back
437 *

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

lines 438–440why, one step back
438 * html: function(){
439 * res.send('<p>hey</p>');440 * },

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 441why, one step back
441 *

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

lines 442–467why, one step back
442 * json: function(){
443 * res.send({ message: 'hey' });444 * }445 * });446 *447 * By default Express passes an `Error`448 * with a `.status` of 406 to `next(err)`449 * if a match is not made. If you provide450 * a `.default` callback it will be invoked451 * instead.452 *453 * @param {Object} obj454 * @return {ServerResponse} for chaining455 * @api public456 */457458res.format = function(obj){459 var req = this.req460 , next = req.next;461462 var fn = obj.default;463 if (fn) delete obj.default;464 var keys = Object.keys(obj);465466 var key = req.accepts(keys);467

add res.vary(). Closes #1682cjihrigbefore adoptione5de08f

line 468why, one step back
468 this.vary("Accept");

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

lines 469–470why, one step back
469
470 if (key) {

Merge pull request #1747 from sorribas/masterTJ Holowaychukbefore adoption1c87e5e

lines 471–472why, one step back
471 var type = normalizeType(key).value;
472 var charset = mime.charsets.lookup(type);

refactor res.format() with a little ocdTJ Holowaychukbefore adoption3b4ce91

line 473why, one step back
473 if (charset) type += '; charset=' + charset;

Merge pull request #1747 from sorribas/masterTJ Holowaychukbefore adoption1c87e5e

line 474why, one step back
474 this.set('Content-Type', type);

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

lines 475–480why, one step back
475 obj[key](req, this, next);
476 } else if (fn) {477 fn();478 } else {479 var err = new Error('Not Acceptable');480 err.status = 406;

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 481why, one step back
481 err.types = normalizeTypes(keys).map(function(o){ return o.value });

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

lines 482–509why, one step back
482 next(err);
483 }484485 return this;486};487488/**489 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.490 *491 * @param {String} filename492 * @return {ServerResponse}493 * @api public494 */495496res.attachment = function(filename){497 if (filename) this.type(extname(filename));498 this.set('Content-Disposition', filename499 ? 'attachment; filename="' + basename(filename) + '"'500 : 'attachment');501 return this;502};503504/**505 * Set header `field` to `val`, or pass506 * an object of header fields.507 *508 * Examples:509 *

refactor res.set() array supportTJ Holowaychukbefore adoptiona4b2e48

line 510why, one step back
510 * res.set('Foo', ['bar', 'baz']);

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

lines 511–513why, one step back
511 * res.set('Accept', 'application/json');
512 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });513 *

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 514why, one step back
514 * Aliased as `res.header()`.

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

line 515why, one step back
515 *

refactor res.set() array supportTJ Holowaychukbefore adoptiona4b2e48

line 516why, one step back
516 * @param {String|Object|Array} field

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

lines 517–521why, one step back
517 * @param {String} val
518 * @return {ServerResponse} for chaining519 * @api public520 */521

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 522why, one step back
522res.set =

refactor res.set() array supportTJ Holowaychukbefore adoptiona4b2e48

line 523why, one step back
523res.header = function(field, val){

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

line 524why, one step back
524 if (2 == arguments.length) {

refactor res.set() array supportTJ Holowaychukbefore adoptiona4b2e48

lines 525–527why, one step back
525 if (Array.isArray(val)) val = val.map(String);
526 else val = String(val);527 this.setHeader(field, val);

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

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

refactor res.set() array supportTJ Holowaychukbefore adoptiona4b2e48

line 530why, one step back
530 this.set(key, field[key]);

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

lines 531–587why, one step back
531 }
532 }533 return this;534};535536/**537 * Get value for header `field`.538 *539 * @param {String} field540 * @return {String}541 * @api public542 */543544res.get = function(field){545 return this.getHeader(field);546};547548/**549 * Clear cookie `name`.550 *551 * @param {String} name552 * @param {Object} options553 * @param {ServerResponse} for chaining554 * @api public555 */556557res.clearCookie = function(name, options){558 var opts = { expires: new Date(1), path: '/' };559 return this.cookie(name, '', options560 ? utils.merge(opts, options)561 : opts);562};563564/**565 * Set cookie `name` to `val`, with the given `options`.566 *567 * Options:568 *569 * - `maxAge` max-age in milliseconds, converted to `expires`570 * - `signed` sign the cookie571 * - `path` defaults to "/"572 *573 * Examples:574 *575 * // "Remember Me" for 15 minutes576 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });577 *578 * // save as above579 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })580 *581 * @param {String} name582 * @param {String|Object} val583 * @param {Options} options584 * @api public585 */586587res.cookie = function(name, val, options){

Merge pull request #1458 from gmethvin/cookie_optionsTJ Holowaychukbefore adoptiond4e56c1

line 588why, one step back
588 options = utils.merge({}, options);

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

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

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

line 592why, one step back
592 if ('number' == typeof val) val = val.toString();

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

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

update connectTJ Holowaychukbefore adoption612fc47

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

Merge pull request #1458 from gmethvin/cookie_optionsTJ Holowaychukbefore adoptiond4e56c1

lines 595–598why, one step back
595 if ('maxAge' in options) {
596 options.expires = new Date(Date.now() + options.maxAge);597 options.maxAge /= 1000;598 }

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

lines 599–603why, one step back
599 if (null == options.path) options.path = '/';
600 this.set('Set-Cookie', cookie.serialize(name, String(val), options));601 return this;602};603

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

line 604why, one step back
604

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

line 605why, one step back
605/**

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

line 606why, one step back
606 * Set the location header to `url`.

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

line 607why, one step back
607 *

Merge pull request #1829 from michaelficarra/patch-1TJ Holowaychukbefore adoptionde54af4

line 608why, one step back
608 * The given `url` can also be "back", which redirects

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

lines 609–612why, one step back
609 * to the _Referrer_ or _Referer_ headers or "/".
610 *611 * Examples:612 *

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

lines 613–615why, one step back
613 * res.location('/foo/bar').;
614 * res.location('http://example.com');615 * res.location('../login'); // /blog/post/1 -> /blog/login

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

lines 616–618why, one step back
616 *
617 * Mounting:618 *

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

lines 619–622why, one step back
619 * When an application is mounted and `res.location()`
620 * is given a path that does _not_ lead with "/" it becomes621 * relative to the mount-point. For example if the application622 * is mounted at "/blog", the following would become "/blog/login".

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

line 623why, one step back
623 *

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

line 624why, one step back
624 * res.location('login');

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

line 625why, one step back
625 *

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

line 626why, one step back
626 * While the leading slash would result in a location of "/login":

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

line 627why, one step back
627 *

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

line 628why, one step back
628 * res.location('/login');

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

lines 629–633why, one step back
629 *
630 * @param {String} url631 * @api public632 */633

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

line 634why, one step back
634res.location = function(url){

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

line 635why, one step back
635 var app = this.app

Merge pull request #1802 from kapouer/patch-1Jonathan Ongbefore adoption2bc703c

lines 636–637why, one step back
636 , req = this.req
637 , path;

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

line 638why, one step back
638

Merge pull request #1829 from michaelficarra/patch-1TJ Holowaychukbefore adoptionde54af4

line 639why, one step back
639 // "back" is an alias for the referrer

ocdTJ Holowaychukbefore adoption8a7a695

line 640why, one step back
640 if ('back' == url) url = req.get('Referrer') || '/';

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

lines 641–645why, one step back
641
642 // relative643 if (!~url.indexOf('://') && 0 != url.indexOf('//')) {644 // relative to path645 if ('.' == url[0]) {

Merge pull request #1802 from kapouer/patch-1Jonathan Ongbefore adoption2bc703c

lines 646–648why, one step back
646 path = req.originalUrl.split('?')[0];
647 path = path + ('/' == path[path.length - 1] ? '' : '/');648 url = resolve(path, url);

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

line 649why, one step back
649 // relative to mount-point

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

line 650why, one step back
650 } else if ('/' != url[0]) {

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

line 651why, one step back
651 path = app.path();

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

lines 652–655why, one step back
652 url = path + '/' + url;
653 }654 }655

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

lines 656–682why, one step back
656 // Respond
657 this.set('Location', url);658 return this;659};660661/**662 * Redirect to the given `url` with optional response `status`663 * defaulting to 302.664 *665 * The resulting `url` is determined by `res.location()`, so666 * it will play nicely with mounted apps, relative paths,667 * `"back"` etc.668 *669 * Examples:670 *671 * res.redirect('/foo/bar');672 * res.redirect('http://example.com');673 * res.redirect(301, 'http://example.com');674 * res.redirect('http://example.com', 301);675 * res.redirect('../login'); // /blog/post/1 -> /blog/login676 *677 * @param {String} url678 * @param {Number} code679 * @api public680 */681682res.redirect = function(url){

lint: remove unused stuffJonathan Ongbefore adoption9bed2b8

line 683why, one step back
683 var head = 'HEAD' == this.req.method

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

lines 684–700why, one step back
684 , status = 302
685 , body;686687 // allow status / url688 if (2 == arguments.length) {689 if ('number' == typeof url) {690 status = url;691 url = arguments[1];692 } else {693 status = arguments[1];694 }695 }696697 // Set location header698 this.location(url);699 url = this.get('Location');700

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

lines 701–703why, one step back
701 // Support text/{plain,html} by default
702 this.format({703 text: function(){

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

line 704why, one step back
704 body = statusCodes[status] + '. Redirecting to ' + encodeURI(url);

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

lines 705–720why, one step back
705 },
706707 html: function(){708 var u = utils.escape(url);709 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';710 },711712 default: function(){713 body = '';714 }715 });716717 // Respond718 this.statusCode = status;719 this.set('Content-Length', Buffer.byteLength(body));720 this.end(head ? null : body);

add res.vary(). Closes #1682cjihrigbefore adoptione5de08f

lines 721–747why, one step back
721};
722723/**724 * Add `field` to Vary. If already present in the Vary set, then725 * this call is simply ignored.726 *727 * @param {Array|String} field728 * @param {ServerResponse} for chaining729 * @api public730 */731732res.vary = function(field){733 var self = this;734735 // nothing736 if (!field) return this;737738 // array739 if (Array.isArray(field)) {740 field.forEach(function(field){741 self.vary(field);742 });743 return;744 }745746 var vary = this.get('Vary');747

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

line 748why, one step back
748 // append

add res.vary(). Closes #1682cjihrigbefore adoptione5de08f

lines 749–758why, one step back
749 if (vary) {
750 vary = vary.split(/ *, */);751 if (!~vary.indexOf(field)) vary.push(field);752 this.set('Vary', vary.join(', '));753 return this;754 }755756 // set757 this.set('Vary', field);758 return this;

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

lines 759–799why, one step back
759};
760761/**762 * Render `view` with the given `options` and optional callback `fn`.763 * When a callback function is given a response will _not_ be made764 * automatically, otherwise a response of _200_ and _text/html_ is given.765 *766 * Options:767 *768 * - `cache` boolean hinting to the engine it should cache769 * - `filename` filename of the view being rendered770 *771 * @param {String} view772 * @param {Object|Function} options or callback function773 * @param {Function} fn774 * @api public775 */776777res.render = function(view, options, fn){778 var self = this779 , options = options || {}780 , req = this.req781 , app = req.app;782783 // support callback function as second arg784 if ('function' == typeof options) {785 fn = options, options = {};786 }787788 // merge res.locals789 options._locals = self.locals;790791 // default callback to respond792 fn = fn || function(err, str){793 if (err) return req.next(err);794 self.send(str);795 };796797 // render798 app.render(view, options, fn);799};