Skip to content
Myr

response.js

This file

Path

lib/response.js

814 lines · 19.2 KiB · JavaScript

Last changed by

Deprecate res.redirect(url, status)Douglas Christopher Wilsonbefore adoption5d03d0e

History of this file

Take it

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

update connectTJ Holowaychukbefore adoption297fb4e

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

deprecate things with depd moduleDouglas Christopher Wilsonbefore adoption9c731f1

line 5why, one step back
5var deprecate = require('depd')('express');

replace utils.escape with html-escapeDouglas Christopher Wilsonbefore adoptionf433b7c

line 6why, one step back
6var escapeHtml = require('escape-html');

use vary@0.1.0Jonathan Ongbefore adoption6d65ae5

line 7why, one step back
7var vary = require('vary');

update connectTJ Holowaychukbefore adoption297fb4e

lines 8–14why, one step back
8var http = require('http')
9 , path = require('path')10 , connect = require('connect')11 , utils = connect.utils12 , sign = require('cookie-signature').sign13 , normalizeType = require('./utils').normalizeType14 , normalizeTypes = require('./utils').normalizeTypes

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

line 15why, one step back
15 , setCharset = require('./utils').setCharset

update connectTJ Holowaychukbefore adoption297fb4e

lines 16–19why, one step back
16 , statusCodes = http.STATUS_CODES
17 , cookie = require('cookie')18 , send = require('send')19 , mime = connect.mime

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

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

update connectTJ Holowaychukbefore adoption297fb4e

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

lint: remove unused stuffJonathan Ongbefore adoption9bed2b8

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

update connectTJ Holowaychukbefore adoption297fb4e

lines 23–60why, one step back
23
24/**25 * Response prototype.26 */2728var res = module.exports = {29 __proto__: http.ServerResponse.prototype30};3132/**33 * Set status `code`.34 *35 * @param {Number} code36 * @return {ServerResponse}37 * @api public38 */3940res.status = function(code){41 this.statusCode = code;42 return this;43};4445/**46 * Set Link header field with the given `links`.47 *48 * Examples:49 *50 * res.links({51 * next: 'http://api.example.com/users?page=2',52 * last: 'http://api.example.com/users?page=5'53 * });54 *55 * @param {Object} links56 * @return {ServerResponse}57 * @api public58 */5960res.links = function(links){

refactor res.links()TJ Holowaychukbefore adoption8e2f538

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

update connectTJ Holowaychukbefore adoption297fb4e

lines 64–87why, one step back
64 return '<' + links[rel] + '>; rel="' + rel + '"';
65 }).join(', '));66};6768/**69 * Send a response.70 *71 * Examples:72 *73 * res.send(new Buffer('wahoo'));74 * res.send({ some: 'json' });75 * res.send('<p>some html</p>');76 * res.send(404, 'Sorry, cant find that');77 * res.send(404);78 *79 * @param {Mixed} body or status80 * @param {Mixed} body81 * @return {ServerResponse}82 * @api public83 */8485res.send = function(body){86 var req = this.req;87 var head = 'HEAD' == req.method;

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 88–89why, one step back
88 var type;
89 var encoding;

update connectTJ Holowaychukbefore adoption297fb4e

lines 90–105why, one step back
90 var len;
9192 // settings93 var app = this.app;9495 // allow status / body96 if (2 == arguments.length) {97 // res.send(body, status) backwards compat98 if ('number' != typeof body && 'number' == typeof arguments[1]) {99 this.statusCode = arguments[1];100 } else {101 this.statusCode = body;102 body = arguments[1];103 }104 }105

Fix res.send(status, num) to send num as jsonYad Smoodbefore adoption544c666

lines 106–113why, one step back
106 // disambiguate res.send(status) and res.send(status, num)
107 if (typeof body === 'number' && arguments.length === 1) {108 // res.send(status) will set status message as text string109 this.get('Content-Type') || this.type('txt');110 this.statusCode = body;111 body = http.STATUS_CODES[body];112 }113

update connectTJ Holowaychukbefore adoption297fb4e

lines 114–122why, one step back
114 switch (typeof body) {
115 // string defaulting to html116 case 'string':117 if (!this.get('Content-Type')) {118 this.charset = this.charset || 'utf-8';119 this.type('html');120 }121 break;122 case 'boolean':

Fix res.send(status, num) to send num as jsonYad Smoodbefore adoption544c666

line 123why, one step back
123 case 'number':

update connectTJ Holowaychukbefore adoption297fb4e

lines 124–134why, one step back
124 case 'object':
125 if (null == body) {126 body = '';127 } else if (Buffer.isBuffer(body)) {128 this.get('Content-Type') || this.type('bin');129 } else {130 return this.json(body);131 }132 break;133 }134

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 135–142why, one step back
135 // write strings in utf-8
136 if ('string' === typeof body) {137 encoding = 'utf8';138 type = this.get('Content-Type');139140 // reflect this in content-type141 if ('string' === typeof type) {142 this.set('Content-Type', setCharset(type, 'utf-8'));

improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a

lines 143–159why, one step back
143 }
144 }145146 // populate Content-Length147 if (undefined !== body && !this.get('Content-Length')) {148 len = Buffer.isBuffer(body)149 ? body.length150 : Buffer.byteLength(body, encoding);151 this.set('Content-Length', len);152 }153154 // ETag support155 var etag = len !== undefined && app.get('etag fn');156 if (etag && ('GET' === req.method || 'HEAD' === req.method)) {157 if (!this.get('ETag')) {158 etag = etag(body, encoding);159 etag && this.set('ETag', etag);

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 160–162why, one step back
160 }
161 }162

update connectTJ Holowaychukbefore adoption297fb4e

lines 163–174why, one step back
163 // freshness
164 if (req.fresh) this.statusCode = 304;165166 // strip irrelevant headers167 if (204 == this.statusCode || 304 == this.statusCode) {168 this.removeHeader('Content-Type');169 this.removeHeader('Content-Length');170 this.removeHeader('Transfer-Encoding');171 body = '';172 }173174 // respond

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 175–176why, one step back
175 this.end((head ? null : body), encoding);
176

update connectTJ Holowaychukbefore adoption297fb4e

lines 177–201why, one step back
177 return this;
178};179180/**181 * Send JSON response.182 *183 * Examples:184 *185 * res.json(null);186 * res.json({ user: 'tj' });187 * res.json(500, 'oh noes!');188 * res.json(404, 'I dont have that');189 *190 * @param {Mixed} obj or status191 * @param {Mixed} obj192 * @return {ServerResponse}193 * @api public194 */195196res.json = function(obj){197 // allow status / body198 if (2 == arguments.length) {199 // res.json(body, status) backwards compat200 if ('number' == typeof arguments[1]) {201 this.statusCode = arguments[1];

deprecate things with depd moduleDouglas Christopher Wilsonbefore adoption9c731f1

lines 202–206why, one step back
202 if (typeof obj === 'number') {
203 deprecate('res.json(obj, status): Use res.json(status, obj) instead');204 } else {205 deprecate('res.json(num, status): Use res.status(status).json(num) instead');206 }

update connectTJ Holowaychukbefore adoption297fb4e

lines 207–247why, one step back
207 } else {
208 this.statusCode = obj;209 obj = arguments[1];210 }211 }212213 // settings214 var app = this.app;215 var replacer = app.get('json replacer');216 var spaces = app.get('json spaces');217 var body = JSON.stringify(obj, replacer, spaces);218219 // content-type220 this.charset = this.charset || 'utf-8';221 this.get('Content-Type') || this.set('Content-Type', 'application/json');222223 return this.send(body);224};225226/**227 * Send JSON response with JSONP callback support.228 *229 * Examples:230 *231 * res.jsonp(null);232 * res.jsonp({ user: 'tj' });233 * res.jsonp(500, 'oh noes!');234 * res.jsonp(404, 'I dont have that');235 *236 * @param {Mixed} obj or status237 * @param {Mixed} obj238 * @return {ServerResponse}239 * @api public240 */241242res.jsonp = function(obj){243 // allow status / body244 if (2 == arguments.length) {245 // res.json(body, status) backwards compat246 if ('number' == typeof arguments[1]) {247 this.statusCode = arguments[1];

deprecate things with depd moduleDouglas Christopher Wilsonbefore adoption9c731f1

lines 248–252why, one step back
248 if (typeof obj === 'number') {
249 deprecate('res.jsonp(obj, status): Use res.jsonp(status, obj) instead');250 } else {251 deprecate('res.jsonp(num, status): Use res.status(status).jsonp(num) instead');252 }

update connectTJ Holowaychukbefore adoption297fb4e

lines 253–269why, one step back
253 } else {
254 this.statusCode = obj;255 obj = arguments[1];256 }257 }258259 // settings260 var app = this.app;261 var replacer = app.get('json replacer');262 var spaces = app.get('json spaces');263 var body = JSON.stringify(obj, replacer, spaces)264 .replace(/\u2028/g, '\\u2028')265 .replace(/\u2029/g, '\\u2029');266 var callback = this.req.query[app.get('jsonp callback name')];267268 // content-type269 this.charset = this.charset || 'utf-8';

Keep previous Content-Type for res.jsonpAlberto Lealbefore adoption084f5d8

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

update connectTJ Holowaychukbefore adoption297fb4e

line 271why, one step back
271

Merge branch '3.5.x' into 3.xDouglas Christopher Wilsonbefore adoption66b38b5

lines 272–276why, one step back
272 // fixup callback
273 if (Array.isArray(callback)) {274 callback = callback[0];275 }276

update connectTJ Holowaychukbefore adoption297fb4e

line 277why, one step back
277 // jsonp

Merge branch '3.5.x' into 3.xDouglas Christopher Wilsonbefore adoption66b38b5

line 278why, one step back
278 if (callback && 'string' === typeof callback) {

update connectTJ Holowaychukbefore adoption297fb4e

lines 279–280why, one step back
279 this.set('Content-Type', 'text/javascript');
280 var cb = callback.replace(/[^\[\]\w$.]/g, '');

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

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

update connectTJ Holowaychukbefore adoption297fb4e

lines 282–349why, one step back
282 }
283284 return this.send(body);285};286287/**288 * Transfer the file at the given `path`.289 *290 * Automatically sets the _Content-Type_ response header field.291 * The callback `fn(err)` is invoked when the transfer is complete292 * or when an error occurs. Be sure to check `res.sentHeader`293 * if you wish to attempt responding, as the header and some data294 * may have already been transferred.295 *296 * Options:297 *298 * - `maxAge` defaulting to 0299 * - `root` root directory for relative filenames300 *301 * Examples:302 *303 * The following example illustrates how `res.sendfile()` may304 * be used as an alternative for the `static()` middleware for305 * dynamic situations. The code backing `res.sendfile()` is actually306 * the same code, so HTTP cache support etc is identical.307 *308 * app.get('/user/:uid/photos/:file', function(req, res){309 * var uid = req.params.uid310 * , file = req.params.file;311 *312 * req.user.mayViewFilesFrom(uid, function(yes){313 * if (yes) {314 * res.sendfile('/uploads/' + uid + '/' + file);315 * } else {316 * res.send(403, 'Sorry! you cant see that.');317 * }318 * });319 * });320 *321 * @param {String} path322 * @param {Object|Function} options or fn323 * @param {Function} fn324 * @api public325 */326327res.sendfile = function(path, options, fn){328 var self = this329 , req = self.req330 , next = this.req.next331 , options = options || {}332 , done;333334 // support function as second arg335 if ('function' == typeof options) {336 fn = options;337 options = {};338 }339340 // socket errors341 req.socket.on('error', error);342343 // errors344 function error(err) {345 if (done) return;346 done = true;347348 // clean up349 cleanup();

update connect to 2.16.1Douglas Christopher Wilsonbefore adoption65f13c3

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

update connectTJ Holowaychukbefore adoption297fb4e

lines 351–355why, one step back
351
352 // callback available353 if (fn) return fn(err);354355 // list in limbo if there's no callback

update connect to 2.16.1Douglas Christopher Wilsonbefore adoption65f13c3

line 356why, one step back
356 if (self.headersSent) return;

update connectTJ Holowaychukbefore adoption297fb4e

lines 357–362why, one step back
357
358 // delegate359 next(err);360 }361362 // streaming

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

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

update connectTJ Holowaychukbefore adoption297fb4e

lines 364–365why, one step back
364 if (done) return;
365 cleanup();

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

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

update connectTJ Holowaychukbefore adoption297fb4e

lines 367–390why, one step back
367 }
368369 // cleanup370 function cleanup() {371 req.socket.removeListener('error', error);372 }373374 // transfer375 var file = send(req, path);376 if (options.root) file.root(options.root);377 file.maxage(options.maxAge || 0);378 file.on('error', error);379 file.on('directory', next);380 file.on('stream', stream);381 file.pipe(this);382 this.on('finish', cleanup);383};384385/**386 * Transfer the file at the given `path` as an attachment.387 *388 * Optionally providing an alternate attachment `filename`,389 * and optional callback `fn(err)`. The callback is invoked390 * when the data transfer is complete, or when an error has

update connect to 2.16.1Douglas Christopher Wilsonbefore adoption65f13c3

line 391why, one step back
391 * ocurred. Be sure to check `res.headersSent` if you plan to respond.

update connectTJ Holowaychukbefore adoption297fb4e

lines 392–503why, one step back
392 *
393 * This method uses `res.sendfile()`.394 *395 * @param {String} path396 * @param {String|Function} filename or fn397 * @param {Function} fn398 * @api public399 */400401res.download = function(path, filename, fn){402 // support function as second arg403 if ('function' == typeof filename) {404 fn = filename;405 filename = null;406 }407408 filename = filename || path;409 this.set('Content-Disposition', 'attachment; filename="' + basename(filename) + '"');410 return this.sendfile(path, fn);411};412413/**414 * Set _Content-Type_ response header with `type` through `mime.lookup()`415 * when it does not contain "/", or set the Content-Type to `type` otherwise.416 *417 * Examples:418 *419 * res.type('.html');420 * res.type('html');421 * res.type('json');422 * res.type('application/json');423 * res.type('png');424 *425 * @param {String} type426 * @return {ServerResponse} for chaining427 * @api public428 */429430res.contentType =431res.type = function(type){432 return this.set('Content-Type', ~type.indexOf('/')433 ? type434 : mime.lookup(type));435};436437/**438 * Respond to the Acceptable formats using an `obj`439 * of mime-type callbacks.440 *441 * This method uses `req.accepted`, an array of442 * acceptable types ordered by their quality values.443 * When "Accept" is not present the _first_ callback444 * is invoked, otherwise the first match is used. When445 * no match is performed the server responds with446 * 406 "Not Acceptable".447 *448 * Content-Type is set for you, however if you choose449 * you may alter this within the callback using `res.type()`450 * or `res.set('Content-Type', ...)`.451 *452 * res.format({453 * 'text/plain': function(){454 * res.send('hey');455 * },456 *457 * 'text/html': function(){458 * res.send('<p>hey</p>');459 * },460 *461 * 'appliation/json': function(){462 * res.send({ message: 'hey' });463 * }464 * });465 *466 * In addition to canonicalized MIME types you may467 * also use extnames mapped to these types:468 *469 * res.format({470 * text: function(){471 * res.send('hey');472 * },473 *474 * html: function(){475 * res.send('<p>hey</p>');476 * },477 *478 * json: function(){479 * res.send({ message: 'hey' });480 * }481 * });482 *483 * By default Express passes an `Error`484 * with a `.status` of 406 to `next(err)`485 * if a match is not made. If you provide486 * a `.default` callback it will be invoked487 * instead.488 *489 * @param {Object} obj490 * @return {ServerResponse} for chaining491 * @api public492 */493494res.format = function(obj){495 var req = this.req496 , next = req.next;497498 var fn = obj.default;499 if (fn) delete obj.default;500 var keys = Object.keys(obj);501502 var key = req.accepts(keys);503

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

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

update connectTJ Holowaychukbefore adoption297fb4e

lines 505–506why, one step back
505
506 if (key) {

Merge pull request #1747 from sorribas/masterTJ Holowaychukbefore adoption1c87e5e

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

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

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

Merge pull request #1747 from sorribas/masterTJ Holowaychukbefore adoption1c87e5e

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

update connectTJ Holowaychukbefore adoption297fb4e

lines 511–643why, one step back
511 obj[key](req, this, next);
512 } else if (fn) {513 fn();514 } else {515 var err = new Error('Not Acceptable');516 err.status = 406;517 err.types = normalizeTypes(keys).map(function(o){ return o.value });518 next(err);519 }520521 return this;522};523524/**525 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.526 *527 * @param {String} filename528 * @return {ServerResponse}529 * @api public530 */531532res.attachment = function(filename){533 if (filename) this.type(extname(filename));534 this.set('Content-Disposition', filename535 ? 'attachment; filename="' + basename(filename) + '"'536 : 'attachment');537 return this;538};539540/**541 * Set header `field` to `val`, or pass542 * an object of header fields.543 *544 * Examples:545 *546 * res.set('Foo', ['bar', 'baz']);547 * res.set('Accept', 'application/json');548 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });549 *550 * Aliased as `res.header()`.551 *552 * @param {String|Object|Array} field553 * @param {String} val554 * @return {ServerResponse} for chaining555 * @api public556 */557558res.set =559res.header = function(field, val){560 if (2 == arguments.length) {561 if (Array.isArray(val)) val = val.map(String);562 else val = String(val);563 this.setHeader(field, val);564 } else {565 for (var key in field) {566 this.set(key, field[key]);567 }568 }569 return this;570};571572/**573 * Get value for header `field`.574 *575 * @param {String} field576 * @return {String}577 * @api public578 */579580res.get = function(field){581 return this.getHeader(field);582};583584/**585 * Clear cookie `name`.586 *587 * @param {String} name588 * @param {Object} options589 * @param {ServerResponse} for chaining590 * @api public591 */592593res.clearCookie = function(name, options){594 var opts = { expires: new Date(1), path: '/' };595 return this.cookie(name, '', options596 ? utils.merge(opts, options)597 : opts);598};599600/**601 * Set cookie `name` to `val`, with the given `options`.602 *603 * Options:604 *605 * - `maxAge` max-age in milliseconds, converted to `expires`606 * - `signed` sign the cookie607 * - `path` defaults to "/"608 *609 * Examples:610 *611 * // "Remember Me" for 15 minutes612 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });613 *614 * // save as above615 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })616 *617 * @param {String} name618 * @param {String|Object} val619 * @param {Options} options620 * @api public621 */622623res.cookie = function(name, val, options){624 options = utils.merge({}, options);625 var secret = this.req.secret;626 var signed = options.signed;627 if (signed && !secret) throw new Error('connect.cookieParser("secret") required for signed cookies');628 if ('number' == typeof val) val = val.toString();629 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);630 if (signed) val = 's:' + sign(val, secret);631 if ('maxAge' in options) {632 options.expires = new Date(Date.now() + options.maxAge);633 options.maxAge /= 1000;634 }635 if (null == options.path) options.path = '/';636 this.set('Set-Cookie', cookie.serialize(name, String(val), options));637 return this;638};639640641/**642 * Set the location header to `url`.643 *

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

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

update connectTJ Holowaychukbefore adoption297fb4e

lines 645–671why, one step back
645 * to the _Referrer_ or _Referer_ headers or "/".
646 *647 * Examples:648 *649 * res.location('/foo/bar').;650 * res.location('http://example.com');651 * res.location('../login'); // /blog/post/1 -> /blog/login652 *653 * Mounting:654 *655 * When an application is mounted and `res.location()`656 * is given a path that does _not_ lead with "/" it becomes657 * relative to the mount-point. For example if the application658 * is mounted at "/blog", the following would become "/blog/login".659 *660 * res.location('login');661 *662 * While the leading slash would result in a location of "/login":663 *664 * res.location('/login');665 *666 * @param {String} url667 * @api public668 */669670res.location = function(url){671 var app = this.app

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

lines 672–673why, one step back
672 , req = this.req
673 , path;

update connectTJ Holowaychukbefore adoption297fb4e

line 674why, one step back
674

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

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

ocdTJ Holowaychukbefore adoption8a7a695

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

update connectTJ Holowaychukbefore adoption297fb4e

lines 677–681why, one step back
677
678 // relative679 if (!~url.indexOf('://') && 0 != url.indexOf('//')) {680 // relative to path681 if ('.' == url[0]) {

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

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

update connectTJ Holowaychukbefore adoption297fb4e

lines 685–715why, one step back
685 // relative to mount-point
686 } else if ('/' != url[0]) {687 path = app.path();688 url = path + '/' + url;689 }690 }691692 // Respond693 this.set('Location', url);694 return this;695};696697/**698 * Redirect to the given `url` with optional response `status`699 * defaulting to 302.700 *701 * The resulting `url` is determined by `res.location()`, so702 * it will play nicely with mounted apps, relative paths,703 * `"back"` etc.704 *705 * Examples:706 *707 * res.redirect('/foo/bar');708 * res.redirect('http://example.com');709 * res.redirect(301, 'http://example.com');710 * res.redirect('../login'); // /blog/post/1 -> /blog/login711 *712 * @api public713 */714715res.redirect = function(url){

lint: remove unused stuffJonathan Ongbefore adoption9bed2b8

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

update connectTJ Holowaychukbefore adoption297fb4e

lines 717–725why, one step back
717 , status = 302
718 , body;719720 // allow status / url721 if (2 == arguments.length) {722 if ('number' == typeof url) {723 status = url;724 url = arguments[1];725 } else {

Deprecate res.redirect(url, status)Douglas Christopher Wilsonbefore adoption5d03d0e

line 726why, one step back
726 deprecate('res.redirect(ur, status): Use res.redirect(status, url) instead');

update connectTJ Holowaychukbefore adoption297fb4e

lines 727–741why, one step back
727 status = arguments[1];
728 }729 }730731 // Set location header732 this.location(url);733 url = this.get('Location');734735 // Support text/{plain,html} by default736 this.format({737 text: function(){738 body = statusCodes[status] + '. Redirecting to ' + encodeURI(url);739 },740741 html: function(){

replace utils.escape with html-escapeDouglas Christopher Wilsonbefore adoptionf433b7c

line 742why, one step back
742 var u = escapeHtml(url);

update connectTJ Holowaychukbefore adoption297fb4e

lines 743–754why, one step back
743 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';
744 },745746 default: function(){747 body = '';748 }749 });750751 // Respond752 this.statusCode = status;753 this.set('Content-Length', Buffer.byteLength(body));754 this.end(head ? null : body);

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

lines 755–766why, one step back
755};
756757/**758 * Add `field` to Vary. If already present in the Vary set, then759 * this call is simply ignored.760 *761 * @param {Array|String} field762 * @param {ServerResponse} for chaining763 * @api public764 */765766res.vary = function(field){

use vary@0.1.0Jonathan Ongbefore adoption6d65ae5

line 767why, one step back
767 // checks for back-compat

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

line 768why, one step back
768 if (!field) return this;

use vary@0.1.0Jonathan Ongbefore adoption6d65ae5

line 769why, one step back
769 if (Array.isArray(field) && !field.length) return this;

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

line 770why, one step back
770

use vary@0.1.0Jonathan Ongbefore adoption6d65ae5

line 771why, one step back
771 vary(this, field);

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

lines 772–773why, one step back
772
773 return this;

update connectTJ Holowaychukbefore adoption297fb4e

lines 774–814why, one step back
774};
775776/**777 * Render `view` with the given `options` and optional callback `fn`.778 * When a callback function is given a response will _not_ be made779 * automatically, otherwise a response of _200_ and _text/html_ is given.780 *781 * Options:782 *783 * - `cache` boolean hinting to the engine it should cache784 * - `filename` filename of the view being rendered785 *786 * @param {String} view787 * @param {Object|Function} options or callback function788 * @param {Function} fn789 * @api public790 */791792res.render = function(view, options, fn){793 var self = this794 , options = options || {}795 , req = this.req796 , app = req.app;797798 // support callback function as second arg799 if ('function' == typeof options) {800 fn = options, options = {};801 }802803 // merge res.locals804 options._locals = self.locals;805806 // default callback to respond807 fn = fn || function(err, str){808 if (err) return req.next(err);809 self.send(str);810 };811812 // render813 app.render(view, options, fn);814};