Skip to content
Myr

response.js

This file

Path

lib/response.js

1,054 lines · 24.3 KiB · JavaScript

Last changed by

Ignore `expires` and `maxAge` in `res.clearCookie()` (#5792)Jon Churchbefore adoption82fc12a

History of this file

Take it

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 1–7why, one step back
1/*!
2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */7

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 8–9why, one step back
8'use strict';
9

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 10–11why, one step back
10/**
11 * Module dependencies.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 12why, one step back
12 * @private

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 13–14why, one step back
13 */
14

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 15why, one step back
15var Buffer = require('safe-buffer').Buffer

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

line 16why, one step back
16var contentDisposition = require('content-disposition');

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 17why, one step back
17var createError = require('http-errors')

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

line 18why, one step back
18var encodeUrl = require('encodeurl');

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 19–20why, one step back
19var escapeHtml = require('escape-html');
20var http = require('http');

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 21why, one step back
21var onFinished = require('on-finished');

Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6

line 22why, one step back
22var mime = require('mime-types')

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 23why, one step back
23var path = require('path');

Use path-is-absolute module for absolute path detectionJeremiah Senkpielbefore adoption694869d

line 24why, one step back
24var pathIsAbsolute = require('path-is-absolute');

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

line 25why, one step back
25var statuses = require('statuses')

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 26why, one step back
26var merge = require('utils-merge');

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 27–33why, one step back
27var sign = require('cookie-signature').sign;
28var normalizeType = require('./utils').normalizeType;29var normalizeTypes = require('./utils').normalizeTypes;30var setCharset = require('./utils').setCharset;31var cookie = require('cookie');32var send = require('send');33var extname = path.extname;

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

line 34why, one step back
34var resolve = path.resolve;

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 35–38why, one step back
35var vary = require('vary');
3637/**38 * Response prototype.

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

line 39why, one step back
39 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 40–41why, one step back
40 */
41

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

line 42why, one step back
42var res = Object.create(http.ServerResponse.prototype)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 43–44why, one step back
43
44/**

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

lines 45–51why, one step back
45 * Module exports.
46 * @public47 */4849module.exports = res5051/**

Merge branch '5.0' into 5.xWes Toddbefore adoption5e2345e

lines 52–58why, one step back
52 * Module variables.
53 * @private54 */5556var schemaAndHostRegExp = /^(?:[a-zA-Z][a-zA-Z0-9+.-]*:)?\/\/[^\\\/\?]+/;5758/**

Throw on invalid status codes (#4212)Jon Churchbefore adoption723b545

line 59why, one step back
59 * Set the HTTP status code for the response.

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 60why, one step back
60 *

Throw on invalid status codes (#4212)Jon Churchbefore adoption723b545

lines 61–67why, one step back
61 * Expects an integer value between 100 and 999 inclusive.
62 * Throws an error if the provided status code is not an integer or if it's outside the allowable range.63 *64 * @param {number} code - The HTTP status code to set.65 * @return {ServerResponse} - Returns itself for chaining methods.66 * @throws {TypeError} If `code` is not an integer.67 * @throws {RangeError} If `code` is outside the range 100 to 999.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 68why, one step back
68 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 69–70why, one step back
69 */
70

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 71why, one step back
71res.status = function status(code) {

Throw on invalid status codes (#4212)Jon Churchbefore adoption723b545

lines 72–74why, one step back
72 // Check if the status code is not an integer
73 if (!Number.isInteger(code)) {74 throw new TypeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be an integer.`);

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 75why, one step back
75 }

Throw on invalid status codes (#4212)Jon Churchbefore adoption723b545

lines 76–80why, one step back
76 // Check if the status code is outside of Node's valid range
77 if (code < 100 || code > 999) {78 throw new RangeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be greater than 99 and less than 1000.`);79 }80

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 81–96why, one step back
81 this.statusCode = code;
82 return this;83};8485/**86 * Set Link header field with the given `links`.87 *88 * Examples:89 *90 * res.links({91 * next: 'http://api.example.com/users?page=2',92 * last: 'http://api.example.com/users?page=5'93 * });94 *95 * @param {Object} links96 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 97why, one step back
97 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 98–112why, one step back
98 */
99100res.links = function(links){101 var link = this.get('Link') || '';102 if (link) link += ', ';103 return this.set('Link', link + Object.keys(links).map(function(rel){104 return '<' + links[rel] + '>; rel="' + rel + '"';105 }).join(', '));106};107108/**109 * Send a response.110 *111 * Examples:112 *

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 113why, one step back
113 * res.send(Buffer.from('wahoo'));

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 114–117why, one step back
114 * res.send({ some: 'json' });
115 * res.send('<p>some html</p>');116 *117 * @param {string|number|boolean|object|Buffer} body

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 118why, one step back
118 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 119–162why, one step back
119 */
120121res.send = function send(body) {122 var chunk = body;123 var encoding;124 var req = this.req;125 var type;126127 // settings128 var app = this.app;129130 switch (typeof chunk) {131 // string defaulting to html132 case 'string':133 if (!this.get('Content-Type')) {134 this.type('html');135 }136 break;137 case 'boolean':138 case 'number':139 case 'object':140 if (chunk === null) {141 chunk = '';142 } else if (Buffer.isBuffer(chunk)) {143 if (!this.get('Content-Type')) {144 this.type('bin');145 }146 } else {147 return this.json(chunk);148 }149 break;150 }151152 // write strings in utf-8153 if (typeof chunk === 'string') {154 encoding = 'utf8';155 type = this.get('Content-Type');156157 // reflect this in content-type158 if (typeof type === 'string') {159 this.set('Content-Type', setCharset(type, 'utf-8'));160 }161 }162

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 163–166why, one step back
163 // determine if ETag should be generated
164 var etagFn = app.get('etag fn')165 var generateETag = !this.get('ETag') && typeof etagFn === 'function'166

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 167why, one step back
167 // populate Content-Length

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 168why, one step back
168 var len

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 169why, one step back
169 if (chunk !== undefined) {

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 170–178why, one step back
170 if (Buffer.isBuffer(chunk)) {
171 // get length of Buffer172 len = chunk.length173 } else if (!generateETag && chunk.length < 1000) {174 // just calculate length when no ETag + small chunk175 len = Buffer.byteLength(chunk, encoding)176 } else {177 // convert chunk to Buffer and calculate178 chunk = Buffer.from(chunk, encoding)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 179why, one step back
179 encoding = undefined;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 180why, one step back
180 len = chunk.length

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 181–185why, one step back
181 }
182183 this.set('Content-Length', len);184 }185

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 186–187why, one step back
186 // populate ETag
187 var etag;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 188–189why, one step back
188 if (generateETag && len !== undefined) {
189 if ((etag = etagFn(chunk, encoding))) {

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 191–194why, one step back
191 }
192 }193194 // freshness

Throw on invalid status codes (#4212)Jon Churchbefore adoption723b545

line 195why, one step back
195 if (req.fresh) this.status(304);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 196–197why, one step back
196
197 // strip irrelevant headers

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

line 198why, one step back
198 if (204 === this.statusCode || 304 === this.statusCode) {

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 199–204why, one step back
199 this.removeHeader('Content-Type');
200 this.removeHeader('Content-Length');201 this.removeHeader('Transfer-Encoding');202 chunk = '';203 }204

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 205–211why, one step back
205 // alter headers for 205
206 if (this.statusCode === 205) {207 this.set('Content-Length', '0')208 this.removeHeader('Transfer-Encoding')209 chunk = ''210 }211

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

line 212why, one step back
212 if (req.method === 'HEAD') {

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 214why, one step back
214 this.end();

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 215–217why, one step back
215 } else {
216 // respond217 this.end(chunk, encoding);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 218–231why, one step back
218 }
219220 return this;221};222223/**224 * Send JSON response.225 *226 * Examples:227 *228 * res.json(null);229 * res.json({ user: 'tj' });230 *231 * @param {string|number|boolean|object} obj

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 232why, one step back
232 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 233–237why, one step back
233 */
234235res.json = function json(obj) {236 // settings237 var app = this.app;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 238why, one step back
238 var escape = app.get('json escape')

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 239–240why, one step back
239 var replacer = app.get('json replacer');
240 var spaces = app.get('json spaces');

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 241why, one step back
241 var body = stringify(obj, replacer, spaces, escape)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 242–259why, one step back
242
243 // content-type244 if (!this.get('Content-Type')) {245 this.set('Content-Type', 'application/json');246 }247248 return this.send(body);249};250251/**252 * Send JSON response with JSONP callback support.253 *254 * Examples:255 *256 * res.jsonp(null);257 * res.jsonp({ user: 'tj' });258 *259 * @param {string|number|boolean|object} obj

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 260why, one step back
260 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 261–265why, one step back
261 */
262263res.jsonp = function jsonp(obj) {264 // settings265 var app = this.app;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 266why, one step back
266 var escape = app.get('json escape')

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 267–268why, one step back
267 var replacer = app.get('json replacer');
268 var spaces = app.get('json spaces');

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 269why, one step back
269 var body = stringify(obj, replacer, spaces, escape)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 270–290why, one step back
270 var callback = this.req.query[app.get('jsonp callback name')];
271272 // content-type273 if (!this.get('Content-Type')) {274 this.set('X-Content-Type-Options', 'nosniff');275 this.set('Content-Type', 'application/json');276 }277278 // fixup callback279 if (Array.isArray(callback)) {280 callback = callback[0];281 }282283 // jsonp284 if (typeof callback === 'string' && callback.length !== 0) {285 this.set('X-Content-Type-Options', 'nosniff');286 this.set('Content-Type', 'text/javascript');287288 // restrict callback charset289 callback = callback.replace(/[^\[\]\w$.]/g, '');290

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

lines 291–299why, one step back
291 if (body === undefined) {
292 // empty argument293 body = ''294 } else if (typeof body === 'string') {295 // replace chars not allowed in JavaScript that are in JSON296 body = body297 .replace(/\u2028/g, '\\u2028')298 .replace(/\u2029/g, '\\u2029')299 }

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 300–306why, one step back
300
301 // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"302 // the typeof check is just to reduce client error noise303 body = '/**/ typeof ' + callback + ' === \'function\' && ' + callback + '(' + body + ');';304 }305306 return this.send(body);

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 307–309why, one step back
307};
308309/**

Add res.sendStatusSeth Samuelbefore adoption12f92a5

lines 310–320why, one step back
310 * Send given HTTP status code.
311 *312 * Sets the response status to `statusCode` and the body of the313 * response to the standard description from node's http.STATUS_CODES314 * or the statusCode number if no description.315 *316 * Examples:317 *318 * res.sendStatus(200);319 *320 * @param {number} statusCode

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 321why, one step back
321 * @public

Add res.sendStatusSeth Samuelbefore adoption12f92a5

lines 322–324why, one step back
322 */
323324res.sendStatus = function sendStatus(statusCode) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 325why, one step back
325 var body = statuses.message[statusCode] || String(statusCode)

Add res.sendStatusSeth Samuelbefore adoption12f92a5

line 326why, one step back
326

Throw on invalid status codes (#4212)Jon Churchbefore adoption723b545

line 327why, one step back
327 this.status(statusCode);

Add res.sendStatusSeth Samuelbefore adoption12f92a5

lines 328–333why, one step back
328 this.type('txt');
329330 return this.send(body);331};332333/**

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 334–336why, one step back
334 * Transfer the file at the given `path`.
335 *336 * Automatically sets the _Content-Type_ response header field.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 337why, one step back
337 * The callback `callback(err)` is invoked when the transfer is complete

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 338why, one step back
338 * or when an error occurs. Be sure to check `res.headersSent`

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 339–370why, one step back
339 * if you wish to attempt responding, as the header and some data
340 * may have already been transferred.341 *342 * Options:343 *344 * - `maxAge` defaulting to 0 (can be string converted by `ms`)345 * - `root` root directory for relative filenames346 * - `headers` object of headers to serve with file347 * - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them348 *349 * Other options are passed along to `send`.350 *351 * Examples:352 *353 * The following example illustrates how `res.sendFile()` may354 * be used as an alternative for the `static()` middleware for355 * dynamic situations. The code backing `res.sendFile()` is actually356 * the same code, so HTTP cache support etc is identical.357 *358 * app.get('/user/:uid/photos/:file', function(req, res){359 * var uid = req.params.uid360 * , file = req.params.file;361 *362 * req.user.mayViewFilesFrom(uid, function(yes){363 * if (yes) {364 * res.sendFile('/uploads/' + uid + '/' + file);365 * } else {366 * res.send(403, 'Sorry! you cant see that.');367 * }368 * });369 * });370 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 371why, one step back
371 * @public

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 372–373why, one step back
372 */
373

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 374–375why, one step back
374res.sendFile = function sendFile(path, options, callback) {
375 var done = callback;

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 376why, one step back
376 var req = this.req;

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 377why, one step back
377 var res = this;

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 378why, one step back
378 var next = req.next;

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 379why, one step back
379 var opts = options || {};

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 380–384why, one step back
380
381 if (!path) {382 throw new TypeError('path argument is required to res.sendFile');383 }384

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 385–388why, one step back
385 if (typeof path !== 'string') {
386 throw new TypeError('path must be a string to res.sendFile')387 }388

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 389–390why, one step back
389 // support function as second arg
390 if (typeof options === 'function') {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 391–392why, one step back
391 done = options;
392 opts = {};

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 393–394why, one step back
393 }
394

Use path-is-absolute module for absolute path detectionJeremiah Senkpielbefore adoption694869d

line 395why, one step back
395 if (!opts.root && !pathIsAbsolute(path)) {

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 396–398why, one step back
396 throw new TypeError('path must be absolute or specify root to res.sendFile');
397 }398

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 399why, one step back
399 // create file stream

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 400why, one step back
400 var pathname = encodeURI(path);

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 401why, one step back
401 var file = send(req, pathname, opts);

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 402why, one step back
402

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 403why, one step back
403 // transfer

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 404–405why, one step back
404 sendfile(res, file, opts, function (err) {
405 if (done) return done(err);

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 406why, one step back
406 if (err && err.code === 'EISDIR') return next();

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 407why, one step back
407

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 408why, one step back
408 // next() all but write errors

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

line 409why, one step back
409 if (err && err.code !== 'ECONNABORTED' && err.syscall !== 'write') {

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 410–412why, one step back
410 next(err);
411 }412 });

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 413–418why, one step back
413};
414415/**416 * Transfer the file at the given `path` as an attachment.417 *418 * Optionally providing an alternate attachment `filename`,

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 419why, one step back
419 * and optional callback `callback(err)`. The callback is invoked

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 420why, one step back
420 * when the data transfer is complete, or when an error has

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 422why, one step back
422 *

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 423–427why, one step back
423 * Optionally providing an `options` object to use with `res.sendFile()`.
424 * This function will set the `Content-Disposition` header, overriding425 * any `Content-Disposition` header passed as header options in order426 * to set the attachment and filename.427 *

Remove res.sendfileDouglas Christopher Wilsonbefore adoption0fc4f07

line 428why, one step back
428 * This method uses `res.sendFile()`.

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 429why, one step back
429 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 430why, one step back
430 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 431–432why, one step back
431 */
432

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 433why, one step back
433res.download = function download (path, filename, options, callback) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 434–435why, one step back
434 var done = callback;
435 var name = filename;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 436why, one step back
436 var opts = options || null

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 437why, one step back
437

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 438why, one step back
438 // support function as second or third arg

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

line 439why, one step back
439 if (typeof filename === 'function') {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 440–441why, one step back
440 done = filename;
441 name = null;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 442–445why, one step back
442 opts = null
443 } else if (typeof options === 'function') {444 done = options445 opts = null

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 446–447why, one step back
446 }
447

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 448–454why, one step back
448 // support optional filename, where options may be in it's place
449 if (typeof filename === 'object' &&450 (typeof options === 'function' || options === undefined)) {451 name = null452 opts = filename453 }454

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 455–456why, one step back
455 // set Content-Disposition when file is sent
456 var headers = {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 457why, one step back
457 'Content-Disposition': contentDisposition(name || path)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 458–459why, one step back
458 };
459

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 460–474why, one step back
460 // merge user-provided headers
461 if (opts && opts.headers) {462 var keys = Object.keys(opts.headers)463 for (var i = 0; i < keys.length; i++) {464 var key = keys[i]465 if (key.toLowerCase() !== 'content-disposition') {466 headers[key] = opts.headers[key]467 }468 }469 }470471 // merge user-provided options472 opts = Object.create(opts)473 opts.headers = headers474

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

line 475why, one step back
475 // Resolve the full path for sendFile

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 476–478why, one step back
476 var fullPath = !opts.root
477 ? resolve(path)478 : path

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

line 479why, one step back
479

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 480–481why, one step back
480 // send file
481 return this.sendFile(fullPath, opts, done)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 482–484why, one step back
482};
483484/**

Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6

line 485why, one step back
485 * Set _Content-Type_ response header with `type` through `mime.contentType()`

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 486why, one step back
486 * when it does not contain "/", or set the Content-Type to `type` otherwise.

Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6

lines 487–488why, one step back
487 * When no mapping is found though `mime.contentType()`, the type is set to
488 * "application/octet-stream".

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 489–499why, one step back
489 *
490 * Examples:491 *492 * res.type('.html');493 * res.type('html');494 * res.type('json');495 * res.type('application/json');496 * res.type('png');497 *498 * @param {String} type499 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 500why, one step back
500 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 501–503why, one step back
501 */
502503res.contentType =

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 504–505why, one step back
504res.type = function contentType(type) {
505 var ct = type.indexOf('/') === -1

Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6

line 506why, one step back
506 ? (mime.contentType(type) || 'application/octet-stream')

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 507–509why, one step back
507 : type;
508509 return this.set('Content-Type', ct);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 510–535why, one step back
510};
511512/**513 * Respond to the Acceptable formats using an `obj`514 * of mime-type callbacks.515 *516 * This method uses `req.accepted`, an array of517 * acceptable types ordered by their quality values.518 * When "Accept" is not present the _first_ callback519 * is invoked, otherwise the first match is used. When520 * no match is performed the server responds with521 * 406 "Not Acceptable".522 *523 * Content-Type is set for you, however if you choose524 * you may alter this within the callback using `res.type()`525 * or `res.set('Content-Type', ...)`.526 *527 * res.format({528 * 'text/plain': function(){529 * res.send('hey');530 * },531 *532 * 'text/html': function(){533 * res.send('<p>hey</p>');534 * },535 *

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 536why, one step back
536 * 'application/json': function () {

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 537–565why, one step back
537 * res.send({ message: 'hey' });
538 * }539 * });540 *541 * In addition to canonicalized MIME types you may542 * also use extnames mapped to these types:543 *544 * res.format({545 * text: function(){546 * res.send('hey');547 * },548 *549 * html: function(){550 * res.send('<p>hey</p>');551 * },552 *553 * json: function(){554 * res.send({ message: 'hey' });555 * }556 * });557 *558 * By default Express passes an `Error`559 * with a `.status` of 406 to `next(err)`560 * if a match is not made. If you provide561 * a `.default` callback it will be invoked562 * instead.563 *564 * @param {Object} obj565 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 566why, one step back
566 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 567–572why, one step back
567 */
568569res.format = function(obj){570 var req = this.req;571 var next = req.next;572

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 573–574why, one step back
573 var keys = Object.keys(obj)
574 .filter(function (v) { return v !== 'default' })

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 575why, one step back
575

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 576–578why, one step back
576 var key = keys.length > 0
577 ? req.accepts(keys)578 : false;

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 579–584why, one step back
579
580 this.vary("Accept");581582 if (key) {583 this.set('Content-Type', normalizeType(key).value);584 obj[key](req, this, next);

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 585–586why, one step back
585 } else if (obj.default) {
586 obj.default(req, this, next)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 587why, one step back
587 } else {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 588–590why, one step back
588 next(createError(406, {
589 types: normalizeTypes(keys).map(function (o) { return o.value })590 }))

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 591–600why, one step back
591 }
592593 return this;594};595596/**597 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.598 *599 * @param {String} filename600 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 601why, one step back
601 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 602–603why, one step back
602 */
603

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

lines 604–608why, one step back
604res.attachment = function attachment(filename) {
605 if (filename) {606 this.type(extname(filename));607 }608

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

line 610why, one step back
610

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 611–614why, one step back
611 return this;
612};613614/**

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 615–625why, one step back
615 * Append additional header `field` with value `val`.
616 *617 * Example:618 *619 * res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);620 * res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');621 * res.append('Warning', '199 Miscellaneous warning');622 *623 * @param {String} field624 * @param {String|Array} val625 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 626why, one step back
626 * @public

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 627–636why, one step back
627 */
628629res.append = function append(field, val) {630 var prev = this.get(field);631 var value = val;632633 if (prev) {634 // concat the new and prev vals635 value = Array.isArray(prev) ? prev.concat(val)636 : Array.isArray(val) ? [prev].concat(val)

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 637why, one step back
637 : [prev, val]

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 638–643why, one step back
638 }
639640 return this.set(field, value);641};642643/**

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 644–654why, one step back
644 * Set header `field` to `val`, or pass
645 * an object of header fields.646 *647 * Examples:648 *649 * res.set('Foo', ['bar', 'baz']);650 * res.set('Accept', 'application/json');651 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });652 *653 * Aliased as `res.header()`.654 *

Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6

lines 655–657why, one step back
655 * When the set header is "Content-Type", the type is expanded to include
656 * the charset if not present using `mime.contentType()`.657 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 658–659why, one step back
658 * @param {String|Object} field
659 * @param {String|Array} val

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 661why, one step back
661 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 662–666why, one step back
662 */
663664res.set =665res.header = function header(field, val) {666 if (arguments.length === 2) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 667–671why, one step back
667 var value = Array.isArray(val)
668 ? val.map(String)669 : String(val);670671 // add charset to content-type

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

lines 672–675why, one step back
672 if (field.toLowerCase() === 'content-type') {
673 if (Array.isArray(value)) {674 throw new TypeError('Content-Type cannot be set to an Array');675 }

Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6

line 676why, one step back
676 value = mime.contentType(value)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 677why, one step back
677 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 678–679why, one step back
678
679 this.setHeader(field, value);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 680–692why, one step back
680 } else {
681 for (var key in field) {682 this.set(key, field[key]);683 }684 }685 return this;686};687688/**689 * Get value for header `field`.690 *691 * @param {String} field692 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 693why, one step back
693 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 694–703why, one step back
694 */
695696res.get = function(field){697 return this.getHeader(field);698};699700/**701 * Clear cookie `name`.702 *703 * @param {String} name

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

line 704why, one step back
704 * @param {Object} [options]

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 706why, one step back
706 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 707–708why, one step back
707 */
708

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 709why, one step back
709res.clearCookie = function clearCookie(name, options) {

Ignore `expires` and `maxAge` in `res.clearCookie()` (#5792)Jon Churchbefore adoption82fc12a

lines 710–713why, one step back
710 // Force cookie expiration by setting expires to the past
711 const opts = { path: '/', ...options, expires: new Date(1)};712 // ensure maxAge is not passed713 delete opts.maxAge

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 714–715why, one step back
714
715 return this.cookie(name, '', opts);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 716–718why, one step back
716};
717718/**

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 719why, one step back
719 * Set cookie `name` to `value`, with the given `options`.

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 720–731why, one step back
720 *
721 * Options:722 *723 * - `maxAge` max-age in milliseconds, converted to `expires`724 * - `signed` sign the cookie725 * - `path` defaults to "/"726 *727 * Examples:728 *729 * // "Remember Me" for 15 minutes730 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });731 *

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

line 732why, one step back
732 * // same as above

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 733–735why, one step back
733 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })
734 *735 * @param {String} name

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 736why, one step back
736 * @param {String|Object} value

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

line 737why, one step back
737 * @param {Object} [options]

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 739why, one step back
739 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 740–741why, one step back
740 */
741

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 742–743why, one step back
742res.cookie = function (name, value, options) {
743 var opts = merge({}, options);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 744why, one step back
744 var secret = this.req.secret;

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 745–748why, one step back
745 var signed = opts.signed;
746747 if (signed && !secret) {748 throw new Error('cookieParser("secret") required for signed cookies');

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 749–750why, one step back
749 }
750

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 751–756why, one step back
751 var val = typeof value === 'object'
752 ? 'j:' + JSON.stringify(value)753 : String(value);754755 if (signed) {756 val = 's:' + sign(val, secret);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 757why, one step back
757 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 758why, one step back
758

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 759–765why, one step back
759 if (opts.maxAge != null) {
760 var maxAge = opts.maxAge - 0761762 if (!isNaN(maxAge)) {763 opts.expires = new Date(Date.now() + maxAge)764 opts.maxAge = Math.floor(maxAge / 1000)765 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 766–773why, one step back
766 }
767768 if (opts.path == null) {769 opts.path = '/';770 }771772 this.append('Set-Cookie', cookie.serialize(name, String(val), opts));773

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 774–790why, one step back
774 return this;
775};776777/**778 * Set the location header to `url`.779 *780 * The given `url` can also be "back", which redirects781 * to the _Referrer_ or _Referer_ headers or "/".782 *783 * Examples:784 *785 * res.location('/foo/bar').;786 * res.location('http://example.com');787 * res.location('../login');788 *789 * @param {String} url790 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 791why, one step back
791 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 792–793why, one step back
792 */
793

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge branch '5.0' into 5.xWes Toddbefore adoption5e2345e

line 795why, one step back
795 var loc;

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 796–797why, one step back
796
797 // "back" is an alias for the referrer

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 798–799why, one step back
798 if (url === 'back') {
799 loc = this.req.get('Referrer') || '/';

Merge branch '5.0' into 5.xWes Toddbefore adoption5e2345e

lines 800–801why, one step back
800 } else {
801 loc = String(url);

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 802why, one step back
802 }

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 803why, one step back
803

Merge branch '5.0' into 5.xWes Toddbefore adoption5e2345e

lines 804–805why, one step back
804 var m = schemaAndHostRegExp.exec(loc);
805 var pos = m ? m[0].length + 1 : 0;

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 806why, one step back
806

Merge branch '5.0' into 5.xWes Toddbefore adoption5e2345e

lines 807–811why, one step back
807 // Only encode after host to avoid invalid encoding which can introduce
808 // vulnerabilities (e.g. `\\` to `%5C`).809 loc = loc.slice(0, pos) + encodeUrl(loc.slice(pos));810811 return this.set('Location', loc);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 812–828why, one step back
812};
813814/**815 * Redirect to the given `url` with optional response `status`816 * defaulting to 302.817 *818 * The resulting `url` is determined by `res.location()`, so819 * it will play nicely with mounted apps, relative paths,820 * `"back"` etc.821 *822 * Examples:823 *824 * res.redirect('/foo/bar');825 * res.redirect('http://example.com');826 * res.redirect(301, 'http://example.com');827 * res.redirect('../login'); // /blog/post/1 -> /blog/login828 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 829why, one step back
829 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 830–838why, one step back
830 */
831832res.redirect = function redirect(url) {833 var address = url;834 var body;835 var status = 302;836837 // allow status / url838 if (arguments.length === 2) {

Remove res.redirect(url, status) signatureMike Tunnicliffebefore adoption71395f5

lines 839–840why, one step back
839 status = arguments[0]
840 address = arguments[1]

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 841–843why, one step back
841 }
842843 // Set location header

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

line 844why, one step back
844 address = this.location(address).get('Location');

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 845–848why, one step back
845
846 // Support text/{plain,html} by default847 this.format({848 text: function(){

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 849why, one step back
849 body = statuses.message[status] + '. Redirecting to ' + address

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 850–852why, one step back
850 },
851852 html: function(){

Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f

line 853why, one step back
853 var u = escapeHtml(address);

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 854why, one step back
854 body = '<p>' + statuses.message[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>'

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 855–862why, one step back
855 },
856857 default: function(){858 body = '';859 }860 });861862 // Respond

Throw on invalid status codes (#4212)Jon Churchbefore adoption723b545

line 863why, one step back
863 this.status(status);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 864–867why, one step back
864 this.set('Content-Length', Buffer.byteLength(body));
865866 if (this.req.method === 'HEAD') {867 this.end();

Merge tag '4.11.2'Douglas Christopher Wilsonbefore adoptiona9ef9e1

lines 868–869why, one step back
868 } else {
869 this.end(body);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 870–878why, one step back
870 }
871};872873/**874 * Add `field` to Vary. If already present in the Vary set, then875 * this call is simply ignored.876 *877 * @param {Array|String} field878 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 879why, one step back
879 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 880–897why, one step back
880 */
881882res.vary = function(field){883 vary(this, field);884885 return this;886};887888/**889 * Render `view` with the given `options` and optional callback `fn`.890 * When a callback function is given a response will _not_ be made891 * automatically, otherwise a response of _200_ and _text/html_ is given.892 *893 * Options:894 *895 * - `cache` boolean hinting to the engine it should cache896 * - `filename` filename of the view being rendered897 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 898why, one step back
898 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 899–900why, one step back
899 */
900

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 901–904why, one step back
901res.render = function render(view, options, callback) {
902 var app = this.req.app;903 var done = callback;904 var opts = options || {};

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 905why, one step back
905 var req = this.req;

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 906why, one step back
906 var self = this;

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 907–908why, one step back
907
908 // support callback function as second arg

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 909–911why, one step back
909 if (typeof options === 'function') {
910 done = options;911 opts = {};

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 912–914why, one step back
912 }
913914 // merge res.locals

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 915why, one step back
915 opts._locals = self.locals;

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 916–917why, one step back
916
917 // default callback to respond

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 918why, one step back
918 done = done || function (err, str) {

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 919–923why, one step back
919 if (err) return req.next(err);
920 self.send(str);921 };922923 // render

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 924why, one step back
924 app.render(view, opts, done);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 925why, one step back
925};

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 926–929why, one step back
926
927// pipe the send file stream928function sendfile(res, file, options, callback) {929 var done = false;

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 930why, one step back
930 var streaming;

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 931why, one step back
931

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 932–937why, one step back
932 // request aborted
933 function onaborted() {934 if (done) return;935 done = true;936937 var err = new Error('Request aborted');

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

line 938why, one step back
938 err.code = 'ECONNABORTED';

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 939–941why, one step back
939 callback(err);
940 }941

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 942–965why, one step back
942 // directory
943 function ondirectory() {944 if (done) return;945 done = true;946947 var err = new Error('EISDIR, read');948 err.code = 'EISDIR';949 callback(err);950 }951952 // errors953 function onerror(err) {954 if (done) return;955 done = true;956 callback(err);957 }958959 // ended960 function onend() {961 if (done) return;962 done = true;963 callback();964 }965

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 966–970why, one step back
966 // file
967 function onfile() {968 streaming = false;969 }970

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 971–972why, one step back
971 // finished
972 function onfinish(err) {

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

line 973why, one step back
973 if (err && err.code === 'ECONNRESET') return onaborted();

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 974–977why, one step back
974 if (err) return onerror(err);
975 if (done) return;976977 setImmediate(function () {

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 978–982why, one step back
978 if (streaming !== false && !done) {
979 onaborted();980 return;981 }982

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 983–984why, one step back
983 if (done) return;
984 done = true;

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 985why, one step back
985 callback();

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 986–988why, one step back
986 });
987 }988

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 989–994why, one step back
989 // streaming
990 function onstream() {991 streaming = true;992 }993994 file.on('directory', ondirectory);

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 995–996why, one step back
995 file.on('end', onend);
996 file.on('error', onerror);

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 997–998why, one step back
997 file.on('file', onfile);
998 file.on('stream', onstream);

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 999–1015why, one step back
999 onFinished(res, onfinish);
10001001 if (options.headers) {1002 // set headers on successful transfer1003 file.on('headers', function headers(res) {1004 var obj = options.headers;1005 var keys = Object.keys(obj);10061007 for (var i = 0; i < keys.length; i++) {1008 var k = keys[i];1009 res.setHeader(k, obj[k]);1010 }1011 });1012 }10131014 // pipe1015 file.pipe(res);

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 1016–1018why, one step back
1016}
10171018/**

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1019–1022why, one step back
1019 * Stringify JSON, like JSON.stringify, but v8 optimized, with the
1020 * ability to escape characters that can trigger HTML sniffing.1021 *1022 * @param {*} value

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 1023why, one step back
1023 * @param {function} replacer

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1024–1026why, one step back
1024 * @param {number} spaces
1025 * @param {boolean} escape1026 * @returns {string}

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 1027–1029why, one step back
1027 * @private
1028 */1029

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 1030why, one step back
1030function stringify (value, replacer, spaces, escape) {

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 1031–1032why, one step back
1031 // v8 checks arguments.length for optimizing simple call
1032 // https://bugs.chromium.org/p/v8/issues/detail?id=4730

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 1033why, one step back
1033 var json = replacer || spaces

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 1034–1035why, one step back
1034 ? JSON.stringify(value, replacer, spaces)
1035 : JSON.stringify(value);

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 1036why, one step back
1036

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 1037why, one step back
1037 if (escape && typeof json === 'string') {

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1038–1045why, one step back
1038 json = json.replace(/[<>&]/g, function (c) {
1039 switch (c.charCodeAt(0)) {1040 case 0x3c:1041 return '\\u003c'1042 case 0x3e:1043 return '\\u003e'1044 case 0x26:1045 return '\\u0026'

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

line 1046why, one step back
1046 /* istanbul ignore next: unreachable default */

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1047–1053why, one step back
1047 default:
1048 return c1049 }1050 })1051 }10521053 return json

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 1054why, one step back
1054}