Skip to content
Myr

response.js

This file

Path

lib/response.js

802 lines · 18.3 KiB · JavaScript

Last changed by

lint: remove unused stuffJonathan Ongbefore adoption9bed2b8

History of this file

Take it

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 10–17why, 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.mime17 , basename = path.basename

lint: remove unused stuffJonathan Ongbefore adoption9bed2b8

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 19–56why, one step back
19
20/**21 * Response prototype.22 */2324var res = module.exports = {25 __proto__: http.ServerResponse.prototype26};2728/**29 * Set status `code`.30 *31 * @param {Number} code32 * @return {ServerResponse}33 * @api public34 */3536res.status = function(code){37 this.statusCode = code;38 return this;39};4041/**42 * Set Link header field with the given `links`.43 *44 * Examples:45 *46 * res.links({47 * next: 'http://api.example.com/users?page=2',48 * last: 'http://api.example.com/users?page=5'49 * });50 *51 * @param {Object} links52 * @return {ServerResponse}53 * @api public54 */5556res.links = function(links){

refactor res.links()TJ Holowaychukbefore adoption8e2f538

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 60–81why, one step back
60 return '<' + links[rel] + '>; rel="' + rel + '"';
61 }).join(', '));62};6364/**65 * Send a response.66 *67 * Examples:68 *69 * res.send(new Buffer('wahoo'));70 * res.send({ some: 'json' });71 * res.send('<p>some html</p>');72 * res.send(404, 'Sorry, cant find that');73 * res.send(404);74 *75 * @param {Mixed} body or status76 * @param {Mixed} body77 * @return {ServerResponse}78 * @api public79 */8081res.send = function(body){

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

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

line 85why, one step back
85

Merge pull request #1694 from kavu/add_disable_etagTJ Holowaychukbefore adoption610e172

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 89–134why, one step back
89 // allow status / body
90 if (2 == arguments.length) {91 // res.send(body, status) backwards compat92 if ('number' != typeof body && 'number' == typeof arguments[1]) {93 this.statusCode = arguments[1];94 } else {95 this.statusCode = body;96 body = arguments[1];97 }98 }99100 switch (typeof body) {101 // response status102 case 'number':103 this.get('Content-Type') || this.type('txt');104 this.statusCode = body;105 body = http.STATUS_CODES[body];106 break;107 // string defaulting to html108 case 'string':109 if (!this.get('Content-Type')) {110 this.charset = this.charset || 'utf-8';111 this.type('html');112 }113 break;114 case 'boolean':115 case 'object':116 if (null == body) {117 body = '';118 } else if (Buffer.isBuffer(body)) {119 this.get('Content-Type') || this.type('bin');120 } else {121 return this.json(body);122 }123 break;124 }125126 // populate Content-Length127 if (undefined !== body && !this.get('Content-Length')) {128 this.set('Content-Length', len = Buffer.isBuffer(body)129 ? body.length130 : Buffer.byteLength(body));131 }132133 // ETag support134 // TODO: W/ support

refactorTJ Holowaychukbefore adoption8ad8cb9

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 136–147why, one step back
136 if (!this.get('ETag')) {
137 this.set('ETag', etag(body));138 }139 }140141 // freshness142 if (req.fresh) this.statusCode = 304;143144 // strip irrelevant headers145 if (204 == this.statusCode || 304 == this.statusCode) {146 this.removeHeader('Content-Type');147 this.removeHeader('Content-Length');

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

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 149–191why, one step back
149 body = '';
150 }151152 // respond153 this.end(head ? null : body);154 return this;155};156157/**158 * Send JSON response.159 *160 * Examples:161 *162 * res.json(null);163 * res.json({ user: 'tj' });164 * res.json(500, 'oh noes!');165 * res.json(404, 'I dont have that');166 *167 * @param {Mixed} obj or status168 * @param {Mixed} obj169 * @return {ServerResponse}170 * @api public171 */172173res.json = function(obj){174 // allow status / body175 if (2 == arguments.length) {176 // res.json(body, status) backwards compat177 if ('number' == typeof arguments[1]) {178 this.statusCode = arguments[1];179 } else {180 this.statusCode = obj;181 obj = arguments[1];182 }183 }184185 // settings186 var app = this.app;187 var replacer = app.get('json replacer');188 var spaces = app.get('json spaces');189 var body = JSON.stringify(obj, replacer, spaces);190191 // content-type

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

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

refactorTJ Holowaychukbefore adoption88072a5

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

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 194why, one step back
194

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 195–229why, one step back
195 return this.send(body);
196};197198/**199 * Send JSON response with JSONP callback support.200 *201 * Examples:202 *203 * res.jsonp(null);204 * res.jsonp({ user: 'tj' });205 * res.jsonp(500, 'oh noes!');206 * res.jsonp(404, 'I dont have that');207 *208 * @param {Mixed} obj or status209 * @param {Mixed} obj210 * @return {ServerResponse}211 * @api public212 */213214res.jsonp = function(obj){215 // allow status / body216 if (2 == arguments.length) {217 // res.json(body, status) backwards compat218 if ('number' == typeof arguments[1]) {219 this.statusCode = arguments[1];220 } else {221 this.statusCode = obj;222 obj = arguments[1];223 }224 }225226 // settings227 var app = this.app;228 var replacer = app.get('json replacer');229 var spaces = app.get('json spaces');

fix jsonp whitespace escape. Closes #1132Guillermo Rauchbefore adoption64a2349

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 238why, one step back
238

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

Merge pull request #1711 from jonjenkins/masterTJ Holowaychukbefore adoption3ea7381

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

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

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

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

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 245–251why, one step back
245 }
246247 return this.send(body);248};249250/**251 * Transfer the file at the given `path`.

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 252why, one step back
252 *

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 274why, one step back
274 *

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 275–325why, one step back
275 * req.user.mayViewFilesFrom(uid, function(yes){
276 * if (yes) {277 * res.sendfile('/uploads/' + uid + '/' + file);278 * } else {279 * res.send(403, 'Sorry! you cant see that.');280 * }281 * });282 * });283 *284 * @param {String} path285 * @param {Object|Function} options or fn286 * @param {Function} fn287 * @api public288 */289290res.sendfile = function(path, options, fn){291 var self = this292 , req = self.req293 , next = this.req.next294 , options = options || {}295 , done;296297 // support function as second arg298 if ('function' == typeof options) {299 fn = options;300 options = {};301 }302303 // socket errors304 req.socket.on('error', error);305306 // errors307 function error(err) {308 if (done) return;309 done = true;310311 // clean up312 cleanup();313 if (!self.headerSent) self.removeHeader('Content-Disposition');314315 // callback available316 if (fn) return fn(err);317318 // list in limbo if there's no callback319 if (self.headerSent) return;320321 // delegate322 next(err);323 }324325 // streaming

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

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

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

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 330–418why, one step back
330 }
331332 // cleanup333 function cleanup() {334 req.socket.removeListener('error', error);335 }336337 // transfer338 var file = send(req, path);339 if (options.root) file.root(options.root);340 file.maxage(options.maxAge || 0);341 file.on('error', error);342 file.on('directory', next);343 file.on('stream', stream);344 file.pipe(this);345 this.on('finish', cleanup);346};347348/**349 * Transfer the file at the given `path` as an attachment.350 *351 * Optionally providing an alternate attachment `filename`,352 * and optional callback `fn(err)`. The callback is invoked353 * when the data transfer is complete, or when an error has354 * ocurred. Be sure to check `res.headerSent` if you plan to respond.355 *356 * This method uses `res.sendfile()`.357 *358 * @param {String} path359 * @param {String|Function} filename or fn360 * @param {Function} fn361 * @api public362 */363364res.download = function(path, filename, fn){365 // support function as second arg366 if ('function' == typeof filename) {367 fn = filename;368 filename = null;369 }370371 filename = filename || path;372 this.set('Content-Disposition', 'attachment; filename="' + basename(filename) + '"');373 return this.sendfile(path, fn);374};375376/**377 * Set _Content-Type_ response header with `type` through `mime.lookup()`378 * when it does not contain "/", or set the Content-Type to `type` otherwise.379 *380 * Examples:381 *382 * res.type('.html');383 * res.type('html');384 * res.type('json');385 * res.type('application/json');386 * res.type('png');387 *388 * @param {String} type389 * @return {ServerResponse} for chaining390 * @api public391 */392393res.contentType =394res.type = function(type){395 return this.set('Content-Type', ~type.indexOf('/')396 ? type397 : mime.lookup(type));398};399400/**401 * Respond to the Acceptable formats using an `obj`402 * of mime-type callbacks.403 *404 * This method uses `req.accepted`, an array of405 * acceptable types ordered by their quality values.406 * When "Accept" is not present the _first_ callback407 * is invoked, otherwise the first match is used. When408 * no match is performed the server responds with409 * 406 "Not Acceptable".410 *411 * Content-Type is set for you, however if you choose412 * you may alter this within the callback using `res.type()`413 * or `res.set('Content-Type', ...)`.414 *415 * res.format({416 * 'text/plain': function(){417 * res.send('hey');418 * },

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 419why, one step back
419 *

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 423why, one step back
423 *

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 436why, one step back
436 *

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 440why, one step back
440 *

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 441–466why, one step back
441 * json: function(){
442 * res.send({ message: 'hey' });443 * }444 * });445 *446 * By default Express passes an `Error`447 * with a `.status` of 406 to `next(err)`448 * if a match is not made. If you provide449 * a `.default` callback it will be invoked450 * instead.451 *452 * @param {Object} obj453 * @return {ServerResponse} for chaining454 * @api public455 */456457res.format = function(obj){458 var req = this.req459 , next = req.next;460461 var fn = obj.default;462 if (fn) delete obj.default;463 var keys = Object.keys(obj);464465 var key = req.accepts(keys);466

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

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

Merge pull request #1747 from sorribas/masterTJ Holowaychukbefore adoption1c87e5e

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

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

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

Merge pull request #1747 from sorribas/masterTJ Holowaychukbefore adoption1c87e5e

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 481–508why, one step back
481 next(err);
482 }483484 return this;485};486487/**488 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.489 *490 * @param {String} filename491 * @return {ServerResponse}492 * @api public493 */494495res.attachment = function(filename){496 if (filename) this.type(extname(filename));497 this.set('Content-Disposition', filename498 ? 'attachment; filename="' + basename(filename) + '"'499 : 'attachment');500 return this;501};502503/**504 * Set header `field` to `val`, or pass505 * an object of header fields.506 *507 * Examples:508 *

refactor res.set() array supportTJ Holowaychukbefore adoptiona4b2e48

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

line 514why, one step back
514 *

refactor res.set() array supportTJ Holowaychukbefore adoptiona4b2e48

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 516–520why, one step back
516 * @param {String} val
517 * @return {ServerResponse} for chaining518 * @api public519 */520

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

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

refactor res.set() array supportTJ Holowaychukbefore adoptiona4b2e48

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

refactor res.set() array supportTJ Holowaychukbefore adoptiona4b2e48

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

refactor res.set() array supportTJ Holowaychukbefore adoptiona4b2e48

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 530–586why, one step back
530 }
531 }532 return this;533};534535/**536 * Get value for header `field`.537 *538 * @param {String} field539 * @return {String}540 * @api public541 */542543res.get = function(field){544 return this.getHeader(field);545};546547/**548 * Clear cookie `name`.549 *550 * @param {String} name551 * @param {Object} options552 * @param {ServerResponse} for chaining553 * @api public554 */555556res.clearCookie = function(name, options){557 var opts = { expires: new Date(1), path: '/' };558 return this.cookie(name, '', options559 ? utils.merge(opts, options)560 : opts);561};562563/**564 * Set cookie `name` to `val`, with the given `options`.565 *566 * Options:567 *568 * - `maxAge` max-age in milliseconds, converted to `expires`569 * - `signed` sign the cookie570 * - `path` defaults to "/"571 *572 * Examples:573 *574 * // "Remember Me" for 15 minutes575 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });576 *577 * // save as above578 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })579 *580 * @param {String} name581 * @param {String|Object} val582 * @param {Options} options583 * @api public584 */585586res.cookie = function(name, val, options){

Merge pull request #1458 from gmethvin/cookie_optionsTJ Holowaychukbefore adoptiond4e56c1

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

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

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

update connectTJ Holowaychukbefore adoption612fc47

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

Merge pull request #1458 from gmethvin/cookie_optionsTJ Holowaychukbefore adoptiond4e56c1

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

line 603why, one step back
603

adjust locals testsTJ Holowaychukbefore adoption1029b0b

line 604why, one step back
604/**

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 606–612why, one step back
606 *
607 * The given `url` can also be the name of a mapped url, for608 * example by default express supports "back" which redirects609 * 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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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".

adjust locals testsTJ Holowaychukbefore adoption1029b0b

line 623why, one step back
623 *

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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":

adjust locals testsTJ Holowaychukbefore adoption1029b0b

line 627why, one step back
627 *

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

line 636why, one step back
636 , req = this.req;

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 637–645why, one step back
637
638 // setup redirect map639 var map = { back: req.get('Referrer') || '/' };640641 // perform redirect642 url = map[url] || url;643644 // relative645 if (!~url.indexOf('://') && 0 != url.indexOf('//')) {

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

line 646why, one step back
646 var path

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 647–649why, one step back
647
648 // relative to path649 if ('.' == url[0]) {

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

lines 650–652why, one step back
650 path = req.originalUrl.split('?')[0]
651 url = path + ('/' == path[path.length - 1] ? '' : '/') + url;652 // relative to mount-point

adjust locals testsTJ Holowaychukbefore adoption1029b0b

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

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

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 655–658why, one step back
655 url = path + '/' + url;
656 }657 }658

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

lines 659–685why, one step back
659 // Respond
660 this.set('Location', url);661 return this;662};663664/**665 * Redirect to the given `url` with optional response `status`666 * defaulting to 302.667 *668 * The resulting `url` is determined by `res.location()`, so669 * it will play nicely with mounted apps, relative paths,670 * `"back"` etc.671 *672 * Examples:673 *674 * res.redirect('/foo/bar');675 * res.redirect('http://example.com');676 * res.redirect(301, 'http://example.com');677 * res.redirect('http://example.com', 301);678 * res.redirect('../login'); // /blog/post/1 -> /blog/login679 *680 * @param {String} url681 * @param {Number} code682 * @api public683 */684685res.redirect = function(url){

lint: remove unused stuffJonathan Ongbefore adoption9bed2b8

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

Merge branch 'integrate'TJ Holowaychukbefore adoption6b1d7a9

lines 687–703why, one step back
687 , status = 302
688 , body;689690 // allow status / url691 if (2 == arguments.length) {692 if ('number' == typeof url) {693 status = url;694 url = arguments[1];695 } else {696 status = arguments[1];697 }698 }699700 // Set location header701 this.location(url);702 url = this.get('Location');703

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 704–706why, one step back
704 // Support text/{plain,html} by default
705 this.format({706 text: function(){

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

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

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 708–723why, one step back
708 },
709710 html: function(){711 var u = utils.escape(url);712 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';713 },714715 default: function(){716 body = '';717 }718 });719720 // Respond721 this.statusCode = status;722 this.set('Content-Length', Buffer.byteLength(body));723 this.end(head ? null : body);

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

lines 724–750why, one step back
724};
725726/**727 * Add `field` to Vary. If already present in the Vary set, then728 * this call is simply ignored.729 *730 * @param {Array|String} field731 * @param {ServerResponse} for chaining732 * @api public733 */734735res.vary = function(field){736 var self = this;737738 // nothing739 if (!field) return this;740741 // array742 if (Array.isArray(field)) {743 field.forEach(function(field){744 self.vary(field);745 });746 return;747 }748749 var vary = this.get('Vary');750

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

line 751why, one step back
751 // append

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

lines 752–761why, one step back
752 if (vary) {
753 vary = vary.split(/ *, */);754 if (!~vary.indexOf(field)) vary.push(field);755 this.set('Vary', vary.join(', '));756 return this;757 }758759 // set760 this.set('Vary', field);761 return this;

adjust locals testsTJ Holowaychukbefore adoption1029b0b

lines 762–802why, one step back
762};
763764/**765 * Render `view` with the given `options` and optional callback `fn`.766 * When a callback function is given a response will _not_ be made767 * automatically, otherwise a response of _200_ and _text/html_ is given.768 *769 * Options:770 *771 * - `cache` boolean hinting to the engine it should cache772 * - `filename` filename of the view being rendered773 *774 * @param {String} view775 * @param {Object|Function} options or callback function776 * @param {Function} fn777 * @api public778 */779780res.render = function(view, options, fn){781 var self = this782 , options = options || {}783 , req = this.req784 , app = req.app;785786 // support callback function as second arg787 if ('function' == typeof options) {788 fn = options, options = {};789 }790791 // merge res.locals792 options._locals = self.locals;793794 // default callback to respond795 fn = fn || function(err, str){796 if (err) return req.next(err);797 self.send(str);798 };799800 // render801 app.render(view, options, fn);802};