Skip to content
Myr

response.js

This file

Path

lib/response.js

831 lines · 19.4 KiB · JavaScript

Last changed by

Merge tag '3.15.0'Douglas Christopher Wilsonbefore adoptionf34944c

History of this file

Take it

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

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

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

Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff

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

Merge tag '3.10.0'Douglas Christopher Wilsonbefore adoption402ec83

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

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 7–12why, one step back
7var http = require('http');
8var path = require('path');9var mixin = require('utils-merge');10var sign = require('cookie-signature').sign;11var normalizeType = require('./utils').normalizeType;12var normalizeTypes = require('./utils').normalizeTypes;

Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061

line 13why, one step back
13var setCharset = require('./utils').setCharset;

Merge pull request #2058 from thetalecrafter/content-dispositionRoman Shtylmanbefore adoptionbdb3bb9

line 14why, one step back
14var contentDisposition = require('./utils').contentDisposition;

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 15–20why, one step back
15var statusCodes = http.STATUS_CODES;
16var cookie = require('cookie');17var send = require('send');18var basename = path.basename;19var extname = path.extname;20var mime = send.mime;

Merge tag '3.10.3'Douglas Christopher Wilsonbefore adoption9bf1247

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

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 22–75why, one step back
22
23/**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/**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 var link = this.get('Link') || '';61 if (link) link += ', ';62 return this.set('Link', link + Object.keys(links).map(function(rel){63 return '<' + links[rel] + '>; rel="' + rel + '"';64 }).join(', '));65};6667/**68 * Send a response.69 *70 * Examples:71 *72 * res.send(new Buffer('wahoo'));73 * res.send({ some: 'json' });74 * res.send('<p>some html</p>');75 *

Deprecate combined status, response signaturesDouglas Christopher Wilsonbefore adoptionfd3b405

line 76why, one step back
76 * @param {string|number|boolean|object|Buffer} body

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 77–79why, one step back
77 * @api public
78 */79

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 80–81why, one step back
80res.send = function send(body) {
81 var chunk = body;

Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061

line 82why, one step back
82 var encoding;

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 83why, one step back
83 var len;

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 84–85why, one step back
84 var req = this.req;
85 var type;

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 86–90why, one step back
86
87 // settings88 var app = this.app;8990 // allow status / body

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

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

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 92why, one step back
92 // res.send(body, status) backwards compat

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 93why, one step back
93 if (typeof arguments[0] !== 'number' && typeof arguments[1] === 'number') {

Deprecate combined status, response signaturesDouglas Christopher Wilsonbefore adoptionfd3b405

line 94why, one step back
94 deprecate('res.send(body, status): Use res.status(status).send(body) instead');

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 95–96why, one step back
95 this.statusCode = arguments[1];
96 } else {

Deprecate combined status, response signaturesDouglas Christopher Wilsonbefore adoptionfd3b405

line 97why, one step back
97 deprecate('res.send(status, body): Use res.status(status).send(body) instead');

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 98–99why, one step back
98 this.statusCode = arguments[0];
99 chunk = arguments[1];

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 100–102why, one step back
100 }
101 }102

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 103why, one step back
103 // disambiguate res.send(status) and res.send(status, num)

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 104why, one step back
104 if (typeof chunk === 'number' && arguments.length === 1) {

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 105why, one step back
105 // res.send(status) will set status message as text string

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 106–109why, one step back
106 if (!this.get('Content-Type')) {
107 this.type('txt');108 }109

Deprecate combined status, response signaturesDouglas Christopher Wilsonbefore adoptionfd3b405

line 110why, one step back
110 deprecate('res.send(status): Use res.status(status).end() instead');

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 111–112why, one step back
111 this.statusCode = chunk;
112 chunk = http.STATUS_CODES[chunk];

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 113–114why, one step back
113 }
114

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 115why, one step back
115 switch (typeof chunk) {

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 116–117why, one step back
116 // string defaulting to html
117 case 'string':

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 118–120why, one step back
118 if (!this.get('Content-Type')) {
119 this.type('html');120 }

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 121–122why, one step back
121 break;
122 case 'boolean':

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

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

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 124why, one step back
124 case 'object':

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 125–130why, one step back
125 if (chunk === null) {
126 chunk = '';127 } else if (Buffer.isBuffer(chunk)) {128 if (!this.get('Content-Type')) {129 this.type('bin');130 }

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 131why, one step back
131 } else {

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 132why, one step back
132 return this.json(chunk);

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 133–136why, one step back
133 }
134 break;135 }136

Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061

line 137why, one step back
137 // write strings in utf-8

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 138why, one step back
138 if (typeof chunk === 'string') {

Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061

lines 139–142why, one step back
139 encoding = 'utf8';
140 type = this.get('Content-Type');141142 // reflect this in content-type

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 143why, one step back
143 if (typeof type === 'string') {

Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061

line 144why, one step back
144 this.set('Content-Type', setCharset(type, 'utf-8'));

Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d

lines 145–148why, one step back
145 }
146 }147148 // populate Content-Length

perf: prevent multiple Buffer creation in res.sendDouglas Christopher Wilsonbefore adoptionb43205c

lines 149–156why, one step back
149 if (chunk !== undefined) {
150 if (!Buffer.isBuffer(chunk)) {151 // convert chunk to Buffer; saves later double conversions152 chunk = new Buffer(chunk, encoding);153 encoding = undefined;154 }155156 len = chunk.length;

Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d

lines 157–159why, one step back
157 this.set('Content-Length', len);
158 }159

perf: prevent multiple Buffer creation in res.sendDouglas Christopher Wilsonbefore adoptionb43205c

lines 160–162why, one step back
160 // method check
161 var isHead = req.method === 'HEAD';162

Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d

line 163why, one step back
163 // ETag support

perf: prevent multiple Buffer creation in res.sendDouglas Christopher Wilsonbefore adoptionb43205c

lines 164–166why, one step back
164 if (len !== undefined && (isHead || req.method === 'GET')) {
165 var etag = app.get('etag fn');166 if (etag && !this.get('ETag')) {

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 167why, one step back
167 etag = etag(chunk, encoding);

Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d

line 168why, one step back
168 etag && this.set('ETag', etag);

Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061

lines 169–171why, one step back
169 }
170 }171

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 172–179why, one step back
172 // freshness
173 if (req.fresh) this.statusCode = 304;174175 // strip irrelevant headers176 if (204 == this.statusCode || 304 == this.statusCode) {177 this.removeHeader('Content-Type');178 this.removeHeader('Content-Length');179 this.removeHeader('Transfer-Encoding');

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 180why, one step back
180 chunk = '';

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

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

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 183why, one step back
183 // skip body for HEAD

perf: prevent multiple Buffer creation in res.sendDouglas Christopher Wilsonbefore adoptionb43205c

line 184why, one step back
184 if (isHead) {

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 185–187why, one step back
185 this.end();
186 }187

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 188why, one step back
188 // respond

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 189why, one step back
189 this.end(chunk, encoding);

Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061

line 190why, one step back
190

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 191–201why, one step back
191 return this;
192};193194/**195 * Send JSON response.196 *197 * Examples:198 *199 * res.json(null);200 * res.json({ user: 'tj' });201 *

Deprecate combined status, response signaturesDouglas Christopher Wilsonbefore adoptionfd3b405

line 202why, one step back
202 * @param {string|number|boolean|object} obj

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 203–205why, one step back
203 * @api public
204 */205

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 206–208why, one step back
206res.json = function json(obj) {
207 var val = obj;208

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 209why, one step back
209 // allow status / body

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

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

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 211why, one step back
211 // res.json(body, status) backwards compat

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 212why, one step back
212 if (typeof arguments[1] === 'number') {

Deprecate combined status, response signaturesDouglas Christopher Wilsonbefore adoptionfd3b405

line 213why, one step back
213 deprecate('res.json(obj, status): Use res.status(status).json(obj) instead');

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 214–215why, one step back
214 this.statusCode = arguments[1];
215 } else {

Deprecate combined status, response signaturesDouglas Christopher Wilsonbefore adoptionfd3b405

line 216why, one step back
216 deprecate('res.json(status, obj): Use res.status(status).json(obj) instead');

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 217–218why, one step back
217 this.statusCode = arguments[0];
218 val = arguments[1];

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 219–225why, one step back
219 }
220 }221222 // settings223 var app = this.app;224 var replacer = app.get('json replacer');225 var spaces = app.get('json spaces');

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 226why, one step back
226 var body = JSON.stringify(val, replacer, spaces);

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 227–228why, one step back
227
228 // content-type

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 229–231why, one step back
229 if (!this.get('Content-Type')) {
230 this.set('Content-Type', 'application/json');231 }

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 232–243why, one step back
232
233 return this.send(body);234};235236/**237 * Send JSON response with JSONP callback support.238 *239 * Examples:240 *241 * res.jsonp(null);242 * res.jsonp({ user: 'tj' });243 *

Deprecate combined status, response signaturesDouglas Christopher Wilsonbefore adoptionfd3b405

line 244why, one step back
244 * @param {string|number|boolean|object} obj

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 245–247why, one step back
245 * @api public
246 */247

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 248–250why, one step back
248res.jsonp = function jsonp(obj) {
249 var val = obj;250

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 251why, one step back
251 // allow status / body

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

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

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 253why, one step back
253 // res.json(body, status) backwards compat

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 254why, one step back
254 if (typeof arguments[1] === 'number') {

Deprecate combined status, response signaturesDouglas Christopher Wilsonbefore adoptionfd3b405

line 255why, one step back
255 deprecate('res.jsonp(obj, status): Use res.status(status).json(obj) instead');

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 256–257why, one step back
256 this.statusCode = arguments[1];
257 } else {

Deprecate combined status, response signaturesDouglas Christopher Wilsonbefore adoptionfd3b405

line 258why, one step back
258 deprecate('res.jsonp(status, obj): Use res.status(status).jsonp(obj) instead');

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 259–260why, one step back
259 this.statusCode = arguments[0];
260 val = arguments[1];

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 261–267why, one step back
261 }
262 }263264 // settings265 var app = this.app;266 var replacer = app.get('json replacer');267 var spaces = app.get('json spaces');

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 268why, one step back
268 var body = JSON.stringify(val, replacer, spaces);

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 269–271why, one step back
269 var callback = this.req.query[app.get('jsonp callback name')];
270271 // content-type

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 272–275why, one step back
272 if (!this.get('Content-Type')) {
273 this.set('X-Content-Type-Options', 'nosniff');274 this.set('Content-Type', 'application/json');275 }

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 276why, one step back
276

Merge branch '4.1.x'Douglas Christopher Wilsonbefore adoption0bbbc84

lines 277–281why, one step back
277 // fixup callback
278 if (Array.isArray(callback)) {279 callback = callback[0];280 }281

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 282why, one step back
282 // jsonp

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 283–285why, one step back
283 if (typeof callback === 'string' && callback.length !== 0) {
284 this.charset = 'utf-8';285 this.set('X-Content-Type-Options', 'nosniff');

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

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

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 287–298why, one step back
287
288 // restrict callback charset289 callback = callback.replace(/[^\[\]\w$.]/g, '');290291 // replace chars not allowed in JavaScript that are in JSON292 body = body293 .replace(/\u2028/g, '\\u2028')294 .replace(/\u2029/g, '\\u2029');295296 // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"297 // the typeof check is just to reduce client error noise298 body = '/**/ typeof ' + callback + ' === \'function\' && ' + callback + '(' + body + ');';

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 299–314why, one step back
299 }
300301 return this.send(body);302};303304/**305 * Transfer the file at the given `path`.306 *307 * Automatically sets the _Content-Type_ response header field.308 * The callback `fn(err)` is invoked when the transfer is complete309 * or when an error occurs. Be sure to check `res.sentHeader`310 * if you wish to attempt responding, as the header and some data311 * may have already been transferred.312 *313 * Options:314 *

Merge tag '3.15.0'Douglas Christopher Wilsonbefore adoptionf34944c

lines 315–318why, one step back
315 * - `maxAge` defaulting to 0 (can be string converted by `ms`)
316 * - `root` root directory for relative filenames317 * - `headers` object of headers to serve with file318 * - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 319why, one step back
319 *

Pass options from res.sendfile to sendDouglas Christopher Wilsonbefore adoption2fd3e72

lines 320–321why, one step back
320 * Other options are passed along to `send`.
321 *

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 322–345why, one step back
322 * Examples:
323 *324 * The following example illustrates how `res.sendfile()` may325 * be used as an alternative for the `static()` middleware for326 * dynamic situations. The code backing `res.sendfile()` is actually327 * the same code, so HTTP cache support etc is identical.328 *329 * app.get('/user/:uid/photos/:file', function(req, res){330 * var uid = req.params.uid331 * , file = req.params.file;332 *333 * req.user.mayViewFilesFrom(uid, function(yes){334 * if (yes) {335 * res.sendfile('/uploads/' + uid + '/' + file);336 * } else {337 * res.send(403, 'Sorry! you cant see that.');338 * }339 * });340 * });341 *342 * @api public343 */344345res.sendfile = function(path, options, fn){

Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874

lines 346–350why, one step back
346 options = options || {};
347 var self = this;348 var req = self.req;349 var next = this.req.next;350 var done;

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 351why, one step back
351

Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874

line 352why, one step back
352

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 353–389why, one step back
353 // support function as second arg
354 if ('function' == typeof options) {355 fn = options;356 options = {};357 }358359 // socket errors360 req.socket.on('error', error);361362 // errors363 function error(err) {364 if (done) return;365 done = true;366367 // clean up368 cleanup();369370 // callback available371 if (fn) return fn(err);372373 // delegate374 next(err);375 }376377 // streaming378 function stream(stream) {379 if (done) return;380 cleanup();381 if (fn) stream.on('end', fn);382 }383384 // cleanup385 function cleanup() {386 req.socket.removeListener('error', error);387 }388389 // transfer

Pass options from res.sendfile to sendDouglas Christopher Wilsonbefore adoption2fd3e72

line 390why, one step back
390 var file = send(req, path, options);

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 391–393why, one step back
391 file.on('error', error);
392 file.on('directory', next);393 file.on('stream', stream);

Add headers option to res.sendfileDouglas Christopher Wilsonbefore adoptionce3d1fe

lines 394–408why, one step back
394
395 if (options.headers) {396 // set headers on successful transfer397 file.on('headers', function headers(res) {398 var obj = options.headers;399 var keys = Object.keys(obj);400401 for (var i = 0; i < keys.length; i++) {402 var k = keys[i];403 res.setHeader(k, obj[k]);404 }405 });406 }407408 // pipe

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 409–433why, one step back
409 file.pipe(this);
410 this.on('finish', cleanup);411};412413/**414 * Transfer the file at the given `path` as an attachment.415 *416 * Optionally providing an alternate attachment `filename`,417 * and optional callback `fn(err)`. The callback is invoked418 * when the data transfer is complete, or when an error has419 * ocurred. Be sure to check `res.headersSent` if you plan to respond.420 *421 * This method uses `res.sendfile()`.422 *423 * @api public424 */425426res.download = function(path, filename, fn){427 // support function as second arg428 if ('function' == typeof filename) {429 fn = filename;430 filename = null;431 }432433 filename = filename || path;

Add headers option to res.sendfileDouglas Christopher Wilsonbefore adoptionce3d1fe

lines 434–440why, one step back
434
435 // set Content-Disposition when file is sent436 var headers = {437 'Content-Disposition': contentDisposition(filename)438 };439440 return this.sendfile(path, { headers: headers }, fn);

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 441–524why, one step back
441};
442443/**444 * Set _Content-Type_ response header with `type` through `mime.lookup()`445 * when it does not contain "/", or set the Content-Type to `type` otherwise.446 *447 * Examples:448 *449 * res.type('.html');450 * res.type('html');451 * res.type('json');452 * res.type('application/json');453 * res.type('png');454 *455 * @param {String} type456 * @return {ServerResponse} for chaining457 * @api public458 */459460res.contentType =461res.type = function(type){462 return this.set('Content-Type', ~type.indexOf('/')463 ? type464 : mime.lookup(type));465};466467/**468 * Respond to the Acceptable formats using an `obj`469 * of mime-type callbacks.470 *471 * This method uses `req.accepted`, an array of472 * acceptable types ordered by their quality values.473 * When "Accept" is not present the _first_ callback474 * is invoked, otherwise the first match is used. When475 * no match is performed the server responds with476 * 406 "Not Acceptable".477 *478 * Content-Type is set for you, however if you choose479 * you may alter this within the callback using `res.type()`480 * or `res.set('Content-Type', ...)`.481 *482 * res.format({483 * 'text/plain': function(){484 * res.send('hey');485 * },486 *487 * 'text/html': function(){488 * res.send('<p>hey</p>');489 * },490 *491 * 'appliation/json': function(){492 * res.send({ message: 'hey' });493 * }494 * });495 *496 * In addition to canonicalized MIME types you may497 * also use extnames mapped to these types:498 *499 * res.format({500 * text: function(){501 * res.send('hey');502 * },503 *504 * html: function(){505 * res.send('<p>hey</p>');506 * },507 *508 * json: function(){509 * res.send({ message: 'hey' });510 * }511 * });512 *513 * By default Express passes an `Error`514 * with a `.status` of 406 to `next(err)`515 * if a match is not made. If you provide516 * a `.default` callback it will be invoked517 * instead.518 *519 * @param {Object} obj520 * @return {ServerResponse} for chaining521 * @api public522 */523524res.format = function(obj){

Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874

lines 525–526why, one step back
525 var req = this.req;
526 var next = req.next;

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 527–560why, one step back
527
528 var fn = obj.default;529 if (fn) delete obj.default;530 var keys = Object.keys(obj);531532 var key = req.accepts(keys);533534 this.vary("Accept");535536 if (key) {537 this.set('Content-Type', normalizeType(key).value);538 obj[key](req, this, next);539 } else if (fn) {540 fn();541 } else {542 var err = new Error('Not Acceptable');543 err.status = 406;544 err.types = normalizeTypes(keys).map(function(o){ return o.value });545 next(err);546 }547548 return this;549};550551/**552 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.553 *554 * @param {String} filename555 * @return {ServerResponse}556 * @api public557 */558559res.attachment = function(filename){560 if (filename) this.type(extname(filename));

Merge pull request #2058 from thetalecrafter/content-dispositionRoman Shtylmanbefore adoptionbdb3bb9

line 561why, one step back
561 this.set('Content-Disposition', contentDisposition(filename));

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 562–583why, one step back
562 return this;
563};564565/**566 * Set header `field` to `val`, or pass567 * an object of header fields.568 *569 * Examples:570 *571 * res.set('Foo', ['bar', 'baz']);572 * res.set('Accept', 'application/json');573 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });574 *575 * Aliased as `res.header()`.576 *577 * @param {String|Object|Array} field578 * @param {String} val579 * @return {ServerResponse} for chaining580 * @api public581 */582583res.set =

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 584–585why, one step back
584res.header = function header(field, val) {
585 if (arguments.length === 2) {

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 586–587why, one step back
586 if (Array.isArray(val)) val = val.map(String);
587 else val = String(val);

preserve casing of headers in res.header and res.setDouglas Christopher Wilsonbefore adoption1536a21

line 588why, one step back
588 if ('content-type' == field.toLowerCase() && !/;\s*charset\s*=/.test(val)) {

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 589–617why, one step back
589 var charset = mime.charsets.lookup(val.split(';')[0]);
590 if (charset) val += '; charset=' + charset.toLowerCase();591 }592 this.setHeader(field, val);593 } else {594 for (var key in field) {595 this.set(key, field[key]);596 }597 }598 return this;599};600601/**602 * Get value for header `field`.603 *604 * @param {String} field605 * @return {String}606 * @api public607 */608609res.get = function(field){610 return this.getHeader(field);611};612613/**614 * Clear cookie `name`.615 *616 * @param {String} name617 * @param {Object} options

build: fix up jsdoc return valuesVasilyev Dmitrybefore adoptionfd86ab8

line 618why, one step back
618 * @return {ServerResponse} for chaining

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 619–648why, one step back
619 * @api public
620 */621622res.clearCookie = function(name, options){623 var opts = { expires: new Date(1), path: '/' };624 return this.cookie(name, '', options625 ? mixin(opts, options)626 : opts);627};628629/**630 * Set cookie `name` to `val`, with the given `options`.631 *632 * Options:633 *634 * - `maxAge` max-age in milliseconds, converted to `expires`635 * - `signed` sign the cookie636 * - `path` defaults to "/"637 *638 * Examples:639 *640 * // "Remember Me" for 15 minutes641 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });642 *643 * // save as above644 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })645 *646 * @param {String} name647 * @param {String|Object} val648 * @param {Options} options

build: fix up jsdoc return valuesVasilyev Dmitrybefore adoptionfd86ab8

line 649why, one step back
649 * @return {ServerResponse} for chaining

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 650–694why, one step back
650 * @api public
651 */652653res.cookie = function(name, val, options){654 options = mixin({}, options);655 var secret = this.req.secret;656 var signed = options.signed;657 if (signed && !secret) throw new Error('cookieParser("secret") required for signed cookies');658 if ('number' == typeof val) val = val.toString();659 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);660 if (signed) val = 's:' + sign(val, secret);661 if ('maxAge' in options) {662 options.expires = new Date(Date.now() + options.maxAge);663 options.maxAge /= 1000;664 }665 if (null == options.path) options.path = '/';666 var headerVal = cookie.serialize(name, String(val), options);667668 // supports multiple 'res.cookie' calls by getting previous value669 var prev = this.get('Set-Cookie');670 if (prev) {671 if (Array.isArray(prev)) {672 headerVal = prev.concat(headerVal);673 } else {674 headerVal = [prev, headerVal];675 }676 }677 this.set('Set-Cookie', headerVal);678 return this;679};680681682/**683 * Set the location header to `url`.684 *685 * The given `url` can also be "back", which redirects686 * to the _Referrer_ or _Referer_ headers or "/".687 *688 * Examples:689 *690 * res.location('/foo/bar').;691 * res.location('http://example.com');692 * res.location('../login');693 *694 * @param {String} url

build: fix up jsdoc return valuesVasilyev Dmitrybefore adoptionfd86ab8

line 695why, one step back
695 * @return {ServerResponse} for chaining

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 696–727why, one step back
696 * @api public
697 */698699res.location = function(url){700 var req = this.req;701702 // "back" is an alias for the referrer703 if ('back' == url) url = req.get('Referrer') || '/';704705 // Respond706 this.set('Location', url);707 return this;708};709710/**711 * Redirect to the given `url` with optional response `status`712 * defaulting to 302.713 *714 * The resulting `url` is determined by `res.location()`, so715 * it will play nicely with mounted apps, relative paths,716 * `"back"` etc.717 *718 * Examples:719 *720 * res.redirect('/foo/bar');721 * res.redirect('http://example.com');722 * res.redirect(301, 'http://example.com');723 * res.redirect('../login'); // /blog/post/1 -> /blog/login724 *725 * @api public726 */727

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 728–729why, one step back
728res.redirect = function redirect(url) {
729 var address = url;

Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874

line 730why, one step back
730 var body;

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

line 731why, one step back
731 var status = 302;

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 732–733why, one step back
732
733 // allow status / url

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 734–737why, one step back
734 if (arguments.length === 2) {
735 if (typeof arguments[0] === 'number') {736 status = arguments[0];737 address = arguments[1];

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 738why, one step back
738 } else {

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

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

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 740–744why, one step back
740 status = arguments[1];
741 }742 }743744 // Set location header

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 745–746why, one step back
745 this.location(address);
746 address = this.get('Location');

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 747–766why, one step back
747
748 // Support text/{plain,html} by default749 this.format({750 text: function(){751 body = statusCodes[status] + '. Redirecting to ' + encodeURI(url);752 },753754 html: function(){755 var u = escapeHtml(url);756 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';757 },758759 default: function(){760 body = '';761 }762 });763764 // Respond765 this.statusCode = status;766 this.set('Content-Length', Buffer.byteLength(body));

perf: fix arguments reassign deopt in some res methodsDouglas Christopher Wilsonbefore adoptionc6e6203

lines 767–772why, one step back
767
768 if (this.req.method === 'HEAD') {769 this.end();770 }771772 this.end(body);

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 773–779why, one step back
773};
774775/**776 * Add `field` to Vary. If already present in the Vary set, then777 * this call is simply ignored.778 *779 * @param {Array|String} field

build: fix up jsdoc return valuesVasilyev Dmitrybefore adoptionfd86ab8

line 780why, one step back
780 * @return {ServerResponse} for chaining

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 781–784why, one step back
781 * @api public
782 */783784res.vary = function(field){

Merge tag '3.10.3'Douglas Christopher Wilsonbefore adoption9bf1247

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

add deprecation message to res.vary()Douglas Christopher Wilsonbefore adoption381b3b0

lines 786–789why, one step back
786 if (!field || (Array.isArray(field) && !field.length)) {
787 deprecate('res.vary(): Provide a field name');788 return this;789 }

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

line 790why, one step back
790

Merge tag '3.10.3'Douglas Christopher Wilsonbefore adoption9bf1247

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

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 792–809why, one step back
792
793 return this;794};795796/**797 * Render `view` with the given `options` and optional callback `fn`.798 * When a callback function is given a response will _not_ be made799 * automatically, otherwise a response of _200_ and _text/html_ is given.800 *801 * Options:802 *803 * - `cache` boolean hinting to the engine it should cache804 * - `filename` filename of the view being rendered805 *806 * @api public807 */808809res.render = function(view, options, fn){

Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874

lines 810–813why, one step back
810 options = options || {};
811 var self = this;812 var req = this.req;813 var app = req.app;

support arrays as middleware arguments to .VERB and .allRoman Shtylmanbefore adoption1eba854

lines 814–831why, one step back
814
815 // support callback function as second arg816 if ('function' == typeof options) {817 fn = options, options = {};818 }819820 // merge res.locals821 options._locals = self.locals;822823 // default callback to respond824 fn = fn || function(err, str){825 if (err) return req.next(err);826 self.send(str);827 };828829 // render830 app.render(view, options, fn);831};