Skip to content
Myr

response.js

This file

Path

lib/response.js

1,039 lines · 24.0 KiB · JavaScript

Last changed by

feat: Extend res.links() to allow adding multiple links with the same rel (closes #2729) (#4885)Andrea Polverinobefore adoptioncaa4f68

History of this file

Take it

The walk stopped after 200 commits: lines older than 6aa4a45 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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

line 19why, one step back
19var http = require('node:http');

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

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

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

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

lines 22–23why, one step back
22var path = require('node:path');
23var pathIsAbsolute = require('node:path').isAbsolute;

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 25–36why, one step back
25var sign = require('cookie-signature').sign;
26var normalizeType = require('./utils').normalizeType;27var normalizeTypes = require('./utils').normalizeTypes;28var setCharset = require('./utils').setCharset;29var cookie = require('cookie');30var send = require('send');31var extname = path.extname;32var resolve = path.resolve;33var vary = require('vary');3435/**36 * Response prototype.

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

line 37why, one step back
37 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 38–39why, one step back
38 */
39

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

lines 43–49why, one step back
43 * Module exports.
44 * @public45 */4647module.exports = res4849/**

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 51why, one step back
51 *

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 59why, one step back
59 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 60–61why, one step back
60 */
61

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

lines 63–65why, one step back
63 // Check if the status code is not an integer
64 if (!Number.isInteger(code)) {65 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 66why, one step back
66 }

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 72–82why, one step back
72 this.statusCode = code;
73 return this;74};7576/**77 * Set Link header field with the given `links`.78 *79 * Examples:80 *81 * res.links({82 * next: 'http://api.example.com/users?page=2',

feat: Extend res.links() to allow adding multiple links with the same rel (closes #2729) (#4885)Andrea Polverinobefore adoptioncaa4f68

lines 83–87why, one step back
83 * last: 'http://api.example.com/users?page=5',
84 * pages: [85 * 'http://api.example.com/users?page=1',86 * 'http://api.example.com/users?page=2'87 * ]

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 88–91why, one step back
88 * });
89 *90 * @param {Object} links91 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 92why, one step back
92 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 93–94why, one step back
93 */
94

feat: Extend res.links() to allow adding multiple links with the same rel (closes #2729) (#4885)Andrea Polverinobefore adoptioncaa4f68

line 95why, one step back
95res.links = function(links) {

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 96–97why, one step back
96 var link = this.get('Link') || '';
97 if (link) link += ', ';

feat: Extend res.links() to allow adding multiple links with the same rel (closes #2729) (#4885)Andrea Polverinobefore adoptioncaa4f68

lines 98–106why, one step back
98 return this.set('Link', link + Object.keys(links).map(function(rel) {
99 // Allow multiple links if links[rel] is an array100 if (Array.isArray(links[rel])) {101 return links[rel].map(function (singleLink) {102 return `<${singleLink}>; rel="${rel}"`;103 }).join(', ');104 } else {105 return `<${links[rel]}>; rel="${rel}"`;106 }

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 107–114why, one step back
107 }).join(', '));
108};109110/**111 * Send a response.112 *113 * Examples:114 *

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 120why, one step back
120 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 121–143why, one step back
121 */
122123res.send = function send(body) {124 var chunk = body;125 var encoding;126 var req = this.req;127 var type;128129 // settings130 var app = this.app;131132 switch (typeof chunk) {133 // string defaulting to html134 case 'string':135 if (!this.get('Content-Type')) {136 this.type('html');137 }138 break;139 case 'boolean':140 case 'number':141 case 'object':142 if (chunk === null) {143 chunk = '';

feat: Added check to support Uint8Array in response sending (#6285)Alexander Ceruttibefore adoption55869f4

line 144why, one step back
144 } else if (ArrayBuffer.isView(chunk)) {

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 145–164why, one step back
145 if (!this.get('Content-Type')) {
146 this.type('bin');147 }148 } else {149 return this.json(chunk);150 }151 break;152 }153154 // write strings in utf-8155 if (typeof chunk === 'string') {156 encoding = 'utf8';157 type = this.get('Content-Type');158159 // reflect this in content-type160 if (typeof type === 'string') {161 this.set('Content-Type', setCharset(type, 'utf-8'));162 }163 }164

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 170why, one step back
170 var len

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 172–180why, one step back
172 if (Buffer.isBuffer(chunk)) {
173 // get length of Buffer174 len = chunk.length175 } else if (!generateETag && chunk.length < 1000) {176 // just calculate length when no ETag + small chunk177 len = Buffer.byteLength(chunk, encoding)178 } else {179 // convert chunk to Buffer and calculate180 chunk = Buffer.from(chunk, encoding)

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 183–187why, one step back
183 }
184185 this.set('Content-Length', len);186 }187

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 188–189why, one step back
188 // populate ETag
189 var etag;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 193–196why, one step back
193 }
194 }195196 // freshness

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 198–199why, one step back
198
199 // strip irrelevant headers

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 217–219why, one step back
217 } else {
218 // respond219 this.end(chunk, encoding);

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 220–233why, one step back
220 }
221222 return this;223};224225/**226 * Send JSON response.227 *228 * Examples:229 *230 * res.json(null);231 * res.json({ user: 'tj' });232 *233 * @param {string|number|boolean|object} obj

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 234why, one step back
234 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 235–239why, one step back
235 */
236237res.json = function json(obj) {238 // settings239 var app = this.app;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 244–261why, one step back
244
245 // content-type246 if (!this.get('Content-Type')) {247 this.set('Content-Type', 'application/json');248 }249250 return this.send(body);251};252253/**254 * Send JSON response with JSONP callback support.255 *256 * Examples:257 *258 * res.jsonp(null);259 * res.jsonp({ user: 'tj' });260 *261 * @param {string|number|boolean|object} obj

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 262why, one step back
262 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 263–267why, one step back
263 */
264265res.jsonp = function jsonp(obj) {266 // settings267 var app = this.app;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 272–292why, one step back
272 var callback = this.req.query[app.get('jsonp callback name')];
273274 // content-type275 if (!this.get('Content-Type')) {276 this.set('X-Content-Type-Options', 'nosniff');277 this.set('Content-Type', 'application/json');278 }279280 // fixup callback281 if (Array.isArray(callback)) {282 callback = callback[0];283 }284285 // jsonp286 if (typeof callback === 'string' && callback.length !== 0) {287 this.set('X-Content-Type-Options', 'nosniff');288 this.set('Content-Type', 'text/javascript');289290 // restrict callback charset291 callback = callback.replace(/[^\[\]\w$.]/g, '');292

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

lines 293–301why, one step back
293 if (body === undefined) {
294 // empty argument295 body = ''296 } else if (typeof body === 'string') {297 // replace chars not allowed in JavaScript that are in JSON298 body = body299 .replace(/\u2028/g, '\\u2028')300 .replace(/\u2029/g, '\\u2029')301 }

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 302–322why, one step back
302
303 // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"304 // the typeof check is just to reduce client error noise305 body = '/**/ typeof ' + callback + ' === \'function\' && ' + callback + '(' + body + ');';306 }307308 return this.send(body);309};310311/**312 * Send given HTTP status code.313 *314 * Sets the response status to `statusCode` and the body of the315 * response to the standard description from node's http.STATUS_CODES316 * or the statusCode number if no description.317 *318 * Examples:319 *320 * res.sendStatus(200);321 *322 * @param {number} statusCode

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 323why, one step back
323 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 328why, one step back
328

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 330–338why, one step back
330 this.type('txt');
331332 return this.send(body);333};334335/**336 * Transfer the file at the given `path`.337 *338 * Automatically sets the _Content-Type_ response header field.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 373why, one step back
373 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 374–375why, one step back
374 */
375

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 378–380why, one step back
378 var req = this.req;
379 var res = this;380 var next = req.next;

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 393–394why, one step back
393 done = options;
394 opts = {};

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 395–396why, one step back
395 }
396

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

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 398–402why, one step back
398 throw new TypeError('path must be absolute or specify root to res.sendFile');
399 }400401 // create file stream402 var pathname = encodeURI(path);

feat: add support for ETag option in res.sendFile (#6073)Juan Josébefore adoption327af12

lines 403–405why, one step back
403
404 // wire application etag option to send405 opts.etag = this.app.enabled('etag');

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 407–408why, one step back
407
408 // transfer

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 411–412why, one step back
411 if (err && err.code === 'EISDIR') return next();
412

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 415–423why, one step back
415 next(err);
416 }417 });418};419420/**421 * Transfer the file at the given `path` as an attachment.422 *423 * Optionally providing an alternate attachment `filename`,

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 427why, one step back
427 *

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 428–432why, one step back
428 * Optionally providing an `options` object to use with `res.sendFile()`.
429 * This function will set the `Content-Disposition` header, overriding430 * any `Content-Disposition` header passed as header options in order431 * to set the attachment and filename.432 *

Remove res.sendfileDouglas Christopher Wilsonbefore adoption0fc4f07

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 434why, one step back
434 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 435why, one step back
435 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 436–437why, one step back
436 */
437

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 439–440why, one step back
439 var done = callback;
440 var name = filename;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 442why, one step back
442

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 445–446why, one step back
445 done = filename;
446 name = null;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 447–450why, one step back
447 opts = null
448 } else if (typeof options === 'function') {449 done = options450 opts = null

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 451–452why, one step back
451 }
452

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 453–459why, one step back
453 // support optional filename, where options may be in it's place
454 if (typeof filename === 'object' &&455 (typeof options === 'function' || options === undefined)) {456 name = null457 opts = filename458 }459

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 463–464why, one step back
463 };
464

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 465–479why, one step back
465 // merge user-provided headers
466 if (opts && opts.headers) {467 var keys = Object.keys(opts.headers)468 for (var i = 0; i < keys.length; i++) {469 var key = keys[i]470 if (key.toLowerCase() !== 'content-disposition') {471 headers[key] = opts.headers[key]472 }473 }474 }475476 // merge user-provided options477 opts = Object.create(opts)478 opts.headers = headers479

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 481–483why, one step back
481 var fullPath = !opts.root
482 ? resolve(path)483 : path

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 484why, one step back
484

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 487–489why, one step back
487};
488489/**

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

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 491why, one step back
491 * 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 492–493why, one step back
492 * When no mapping is found though `mime.contentType()`, the type is set to
493 * "application/octet-stream".

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 494–504why, one step back
494 *
495 * Examples:496 *497 * res.type('.html');498 * res.type('html');499 * res.type('json');500 * res.type('application/json');501 * res.type('png');502 *503 * @param {String} type504 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 505why, one step back
505 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 506–508why, one step back
506 */
507508res.contentType =

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

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

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 512–514why, one step back
512 : type;
513514 return this.set('Content-Type', ct);

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 515–540why, one step back
515};
516517/**518 * Respond to the Acceptable formats using an `obj`519 * of mime-type callbacks.520 *521 * This method uses `req.accepted`, an array of522 * acceptable types ordered by their quality values.523 * When "Accept" is not present the _first_ callback524 * is invoked, otherwise the first match is used. When525 * no match is performed the server responds with526 * 406 "Not Acceptable".527 *528 * Content-Type is set for you, however if you choose529 * you may alter this within the callback using `res.type()`530 * or `res.set('Content-Type', ...)`.531 *532 * res.format({533 * 'text/plain': function(){534 * res.send('hey');535 * },536 *537 * 'text/html': function(){538 * res.send('<p>hey</p>');539 * },540 *

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 571why, one step back
571 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 572–577why, one step back
572 */
573574res.format = function(obj){575 var req = this.req;576 var next = req.next;577

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 580why, one step back
580

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 581–583why, one step back
581 var key = keys.length > 0
582 ? req.accepts(keys)583 : false;

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 584–589why, one step back
584
585 this.vary("Accept");586587 if (key) {588 this.set('Content-Type', normalizeType(key).value);589 obj[key](req, this, next);

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 592why, one step back
592 } else {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 596–605why, one step back
596 }
597598 return this;599};600601/**602 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.603 *604 * @param {String} filename605 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 606why, one step back
606 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 607–608why, one step back
607 */
608

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

lines 609–613why, one step back
609res.attachment = function attachment(filename) {
610 if (filename) {611 this.type(extname(filename));612 }613

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

line 615why, one step back
615

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 616–619why, one step back
616 return this;
617};618619/**

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 631why, one step back
631 * @public

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 632–641why, one step back
632 */
633634res.append = function append(field, val) {635 var prev = this.get(field);636 var value = val;637638 if (prev) {639 // concat the new and prev vals640 value = Array.isArray(prev) ? prev.concat(val)641 : Array.isArray(val) ? [prev].concat(val)

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 643–648why, one step back
643 }
644645 return this.set(field, value);646};647648/**

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

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

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 666why, one step back
666 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 667–671why, one step back
667 */
668669res.set =670res.header = function header(field, val) {671 if (arguments.length === 2) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 672–676why, one step back
672 var value = Array.isArray(val)
673 ? val.map(String)674 : String(val);675676 // add charset to content-type

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

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

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

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 682why, one step back
682 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 683–684why, one step back
683
684 this.setHeader(field, value);

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 685–697why, one step back
685 } else {
686 for (var key in field) {687 this.set(key, field[key]);688 }689 }690 return this;691};692693/**694 * Get value for header `field`.695 *696 * @param {String} field697 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 698why, one step back
698 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 699–708why, one step back
699 */
700701res.get = function(field){702 return this.getHeader(field);703};704705/**706 * Clear cookie `name`.707 *708 * @param {String} name

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 711why, one step back
711 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 712–713why, one step back
712 */
713

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

lines 715–718why, one step back
715 // Force cookie expiration by setting expires to the past
716 const opts = { path: '/', ...options, expires: new Date(1)};717 // ensure maxAge is not passed718 delete opts.maxAge

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 721–723why, one step back
721};
722723/**

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 725–736why, one step back
725 *
726 * Options:727 *728 * - `maxAge` max-age in milliseconds, converted to `expires`729 * - `signed` sign the cookie730 * - `path` defaults to "/"731 *732 * Examples:733 *734 * // "Remember Me" for 15 minutes735 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });736 *

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 744why, one step back
744 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 745–746why, one step back
745 */
746

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 747why, one step back
747res.cookie = function (name, value, options) {

fix: Remove `utils-merge` dependency - use spread syntax instead (#6091)Phillip Bartabefore adoption246f6f5

line 748why, one step back
748 var opts = { ...options };

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 754–755why, one step back
754 }
755

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 762why, one step back
762 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 763why, one step back
763

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 771–778why, one step back
771 }
772773 if (opts.path == null) {774 opts.path = '/';775 }776777 this.append('Set-Cookie', cookie.serialize(name, String(val), opts));778

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 779–795why, one step back
779 return this;
780};781782/**783 * Set the location header to `url`.784 *785 * The given `url` can also be "back", which redirects786 * to the _Referrer_ or _Referer_ headers or "/".787 *788 * Examples:789 *790 * res.location('/foo/bar').;791 * res.location('http://example.com');792 * res.location('../login');793 *794 * @param {String} url795 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 796why, one step back
796 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 797–798why, one step back
797 */
798

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Delete `back` as a magic string (#5933)Blake Embreybefore adoptionbdd81f8

line 800why, one step back
800 return this.set('Location', encodeUrl(url));

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 801–813why, one step back
801};
802803/**804 * Redirect to the given `url` with optional response `status`805 * defaulting to 302.806 *807 * Examples:808 *809 * res.redirect('/foo/bar');810 * res.redirect('http://example.com');811 * res.redirect(301, 'http://example.com');812 * res.redirect('../login'); // /blog/post/1 -> /blog/login813 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 814why, one step back
814 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 815–823why, one step back
815 */
816817res.redirect = function redirect(url) {818 var address = url;819 var body;820 var status = 302;821822 // allow status / url823 if (arguments.length === 2) {

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

lines 824–825why, one step back
824 status = arguments[0]
825 address = arguments[1]

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 826–828why, one step back
826 }
827828 // Set location header

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 830–833why, one step back
830
831 // Support text/{plain,html} by default832 this.format({833 text: function(){

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 835–837why, one step back
835 },
836837 html: function(){

Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f

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

Merge tag '4.20.0' into 5.0Wes Toddbefore adoptione5feb9f

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 840–847why, one step back
840 },
841842 default: function(){843 body = '';844 }845 });846847 // Respond

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.11.2'Douglas Christopher Wilsonbefore adoptiona9ef9e1

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 855–863why, one step back
855 }
856};857858/**859 * Add `field` to Vary. If already present in the Vary set, then860 * this call is simply ignored.861 *862 * @param {Array|String} field863 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 864why, one step back
864 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 865–882why, one step back
865 */
866867res.vary = function(field){868 vary(this, field);869870 return this;871};872873/**874 * Render `view` with the given `options` and optional callback `fn`.875 * When a callback function is given a response will _not_ be made876 * automatically, otherwise a response of _200_ and _text/html_ is given.877 *878 * Options:879 *880 * - `cache` boolean hinting to the engine it should cache881 * - `filename` filename of the view being rendered882 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 883why, one step back
883 * @public

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 884–885why, one step back
884 */
885

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 886–889why, one step back
886res.render = function render(view, options, callback) {
887 var app = this.req.app;888 var done = callback;889 var opts = options || {};

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 892–893why, one step back
892
893 // support callback function as second arg

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 894–896why, one step back
894 if (typeof options === 'function') {
895 done = options;896 opts = {};

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 897–899why, one step back
897 }
898899 // merge res.locals

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 901–902why, one step back
901
902 // default callback to respond

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 904–908why, one step back
904 if (err) return req.next(err);
905 self.send(str);906 };907908 // render

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 910–914why, one step back
910};
911912// pipe the send file stream913function sendfile(res, file, options, callback) {914 var done = false;

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 915why, one step back
915 var streaming;

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 916why, one step back
916

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 917–922why, one step back
917 // request aborted
918 function onaborted() {919 if (done) return;920 done = true;921922 var err = new Error('Request aborted');

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 924–926why, one step back
924 callback(err);
925 }926

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 927–950why, one step back
927 // directory
928 function ondirectory() {929 if (done) return;930 done = true;931932 var err = new Error('EISDIR, read');933 err.code = 'EISDIR';934 callback(err);935 }936937 // errors938 function onerror(err) {939 if (done) return;940 done = true;941 callback(err);942 }943944 // ended945 function onend() {946 if (done) return;947 done = true;948 callback();949 }950

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 951–955why, one step back
951 // file
952 function onfile() {953 streaming = false;954 }955

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 956–957why, one step back
956 // finished
957 function onfinish(err) {

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 959–962why, one step back
959 if (err) return onerror(err);
960 if (done) return;961962 setImmediate(function () {

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 963–967why, one step back
963 if (streaming !== false && !done) {
964 onaborted();965 return;966 }967

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 968–969why, one step back
968 if (done) return;
969 done = true;

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 971–973why, one step back
971 });
972 }973

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 974–979why, one step back
974 // streaming
975 function onstream() {976 streaming = true;977 }978979 file.on('directory', ondirectory);

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 984–1000why, one step back
984 onFinished(res, onfinish);
985986 if (options.headers) {987 // set headers on successful transfer988 file.on('headers', function headers(res) {989 var obj = options.headers;990 var keys = Object.keys(obj);991992 for (var i = 0; i < keys.length; i++) {993 var k = keys[i];994 res.setHeader(k, obj[k]);995 }996 });997 }998999 // pipe1000 file.pipe(res);

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 1001–1003why, one step back
1001}
10021003/**

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1009–1011why, one step back
1009 * @param {number} spaces
1010 * @param {boolean} escape1011 * @returns {string}

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 1012–1014why, one step back
1012 * @private
1013 */1014

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 1021why, one step back
1021

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1023–1030why, one step back
1023 json = json.replace(/[<>&]/g, function (c) {
1024 switch (c.charCodeAt(0)) {1025 case 0x3c:1026 return '\\u003c'1027 case 0x3e:1028 return '\\u003e'1029 case 0x26:1030 return '\\u0026'

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1032–1038why, one step back
1032 default:
1033 return c1034 }1035 })1036 }10371038 return json

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 1039why, one step back
1039}