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 fileTake 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 back1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */7Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 8–9why, one step back8'use strict';9tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 10–11why, one step back10/**11 * Module dependencies.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 12why, one step back12 * @privatetests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 13–14why, one step back13 */14Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 15why, one step back15var Buffer = require('safe-buffer').BufferUse content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 16why, one step back16var contentDisposition = require('content-disposition');Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 17why, one step back17var createError = require('http-errors')Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 18why, one step back18var encodeUrl = require('encodeurl');tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 19–20why, one step back19var escapeHtml = require('escape-html');20var http = require('http');Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 21why, one step back21var onFinished = require('on-finished');Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 22why, one step back22var mime = require('mime-types')tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 23why, one step back23var path = require('path');Use path-is-absolute module for absolute path detectionJeremiah Senkpielbefore adoption694869d
line 24why, one step back24var pathIsAbsolute = require('path-is-absolute');Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 25why, one step back25var statuses = require('statuses')Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 26why, one step back26var merge = require('utils-merge');tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 27–33why, one step back27var 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 back34var resolve = path.resolve;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 35–38why, one step back35var vary = require('vary');3637/**38 * Response prototype.Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 39why, one step back39 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 40–41why, one step back40 */41Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 42why, one step back42var res = Object.create(http.ServerResponse.prototype)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 43–44why, one step back4344/**Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 45–51why, one step back45 * Module exports.46 * @public47 */4849module.exports = res5051/**Merge branch '5.0' into 5.xWes Toddbefore adoption5e2345e
lines 52–58why, one step back52 * 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 back59 * Set the HTTP status code for the response.tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 60why, one step back60 *Throw on invalid status codes (#4212)Jon Churchbefore adoption723b545
lines 61–67why, one step back61 * 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 back68 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 69–70why, one step back69 */70Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 71why, one step back71res.status = function status(code) {Throw on invalid status codes (#4212)Jon Churchbefore adoption723b545
lines 72–74why, one step back72 // Check if the status code is not an integer73 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 back75 }Throw on invalid status codes (#4212)Jon Churchbefore adoption723b545
lines 76–80why, one step back76 // Check if the status code is outside of Node's valid range77 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 }80tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 81–96why, one step back81 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 back97 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 98–112why, one step back98 */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 back113 * res.send(Buffer.from('wahoo'));tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 114–117why, one step back114 * res.send({ some: 'json' });115 * res.send('<p>some html</p>');116 *117 * @param {string|number|boolean|object|Buffer} bodyMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 118why, one step back118 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 119–162why, one step back119 */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 }162Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 163–166why, one step back163 // determine if ETag should be generated164 var etagFn = app.get('etag fn')165 var generateETag = !this.get('ETag') && typeof etagFn === 'function'166tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 167why, one step back167 // populate Content-LengthMerge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 168why, one step back168 var lentests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 169why, one step back169 if (chunk !== undefined) {Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 170–178why, one step back170 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 back179 encoding = undefined;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 180why, one step back180 len = chunk.lengthtests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 181–185why, one step back181 }182183 this.set('Content-Length', len);184 }185Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 186–187why, one step back186 // populate ETag187 var etag;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 188–189why, one step back188 if (generateETag && len !== undefined) {189 if ((etag = etagFn(chunk, encoding))) {Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 190why, one step back190 this.set('ETag', etag);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 191–194why, one step back191 }192 }193194 // freshnessThrow on invalid status codes (#4212)Jon Churchbefore adoption723b545
line 195why, one step back195 if (req.fresh) this.status(304);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 196–197why, one step back196197 // strip irrelevant headersMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 198why, one step back198 if (204 === this.statusCode || 304 === this.statusCode) {tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 199–204why, one step back199 this.removeHeader('Content-Type');200 this.removeHeader('Content-Length');201 this.removeHeader('Transfer-Encoding');202 chunk = '';203 }204Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 205–211why, one step back205 // alter headers for 205206 if (this.statusCode === 205) {207 this.set('Content-Length', '0')208 this.removeHeader('Transfer-Encoding')209 chunk = ''210 }211Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 212why, one step back212 if (req.method === 'HEAD') {Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 213why, one step back213 // skip body for HEADtests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 214why, one step back214 this.end();Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 215–217why, one step back215 } else {216 // respond217 this.end(chunk, encoding);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 218–231why, one step back218 }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} objMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 232why, one step back232 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 233–237why, one step back233 */234235res.json = function json(obj) {236 // settings237 var app = this.app;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 238why, one step back238 var escape = app.get('json escape')tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 239–240why, one step back239 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 back241 var body = stringify(obj, replacer, spaces, escape)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 242–259why, one step back242243 // 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} objMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 260why, one step back260 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 261–265why, one step back261 */262263res.jsonp = function jsonp(obj) {264 // settings265 var app = this.app;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 266why, one step back266 var escape = app.get('json escape')tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 267–268why, one step back267 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 back269 var body = stringify(obj, replacer, spaces, escape)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 270–290why, one step back270 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, '');290Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
lines 291–299why, one step back291 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 back300301 // 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 back307};308309/**Add res.sendStatusSeth Samuelbefore adoption12f92a5
lines 310–320why, one step back310 * 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} statusCodeMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 321why, one step back321 * @publicAdd res.sendStatusSeth Samuelbefore adoption12f92a5
lines 322–324why, one step back322 */323324res.sendStatus = function sendStatus(statusCode) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 325why, one step back325 var body = statuses.message[statusCode] || String(statusCode)Add res.sendStatusSeth Samuelbefore adoption12f92a5
line 326why, one step back326Throw on invalid status codes (#4212)Jon Churchbefore adoption723b545
line 327why, one step back327 this.status(statusCode);Add res.sendStatusSeth Samuelbefore adoption12f92a5
lines 328–333why, one step back328 this.type('txt');329330 return this.send(body);331};332333/**Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 334–336why, one step back334 * 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 back337 * The callback `callback(err)` is invoked when the transfer is completeMerge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 338why, one step back338 * or when an error occurs. Be sure to check `res.headersSent`Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 339–370why, one step back339 * if you wish to attempt responding, as the header and some data340 * 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 back371 * @publicAdd res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 372–373why, one step back372 */373Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 374–375why, one step back374res.sendFile = function sendFile(path, options, callback) {375 var done = callback;Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 376why, one step back376 var req = this.req;Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 377why, one step back377 var res = this;Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 378why, one step back378 var next = req.next;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 379why, one step back379 var opts = options || {};Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 380–384why, one step back380381 if (!path) {382 throw new TypeError('path argument is required to res.sendFile');383 }384Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 385–388why, one step back385 if (typeof path !== 'string') {386 throw new TypeError('path must be a string to res.sendFile')387 }388Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 389–390why, one step back389 // support function as second arg390 if (typeof options === 'function') {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 391–392why, one step back391 done = options;392 opts = {};Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 393–394why, one step back393 }394Use path-is-absolute module for absolute path detectionJeremiah Senkpielbefore adoption694869d
line 395why, one step back395 if (!opts.root && !pathIsAbsolute(path)) {Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 396–398why, one step back396 throw new TypeError('path must be absolute or specify root to res.sendFile');397 }398Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 399why, one step back399 // create file streamAdd res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 400why, one step back400 var pathname = encodeURI(path);Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 401why, one step back401 var file = send(req, pathname, opts);Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 402why, one step back402Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 403why, one step back403 // transferMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 404–405why, one step back404 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 back406 if (err && err.code === 'EISDIR') return next();Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 407why, one step back407Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 408why, one step back408 // next() all but write errorsMerge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 409why, one step back409 if (err && err.code !== 'ECONNABORTED' && err.syscall !== 'write') {Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 410–412why, one step back410 next(err);411 }412 });tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 413–418why, one step back413};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 back419 * and optional callback `callback(err)`. The callback is invokedtests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 420why, one step back420 * when the data transfer is complete, or when an error hasMerge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 421why, one step back421 * 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 back422 *Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 423–427why, one step back423 * 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 back428 * This method uses `res.sendFile()`.tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 429why, one step back429 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 430why, one step back430 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 431–432why, one step back431 */432Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 433why, one step back433res.download = function download (path, filename, options, callback) {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 434–435why, one step back434 var done = callback;435 var name = filename;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 436why, one step back436 var opts = options || nullMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 437why, one step back437Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 438why, one step back438 // support function as second or third argFix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7
line 439why, one step back439 if (typeof filename === 'function') {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 440–441why, one step back440 done = filename;441 name = null;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 442–445why, one step back442 opts = null443 } else if (typeof options === 'function') {444 done = options445 opts = nulltests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 446–447why, one step back446 }447Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 448–454why, one step back448 // support optional filename, where options may be in it's place449 if (typeof filename === 'object' &&450 (typeof options === 'function' || options === undefined)) {451 name = null452 opts = filename453 }454tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 455–456why, one step back455 // set Content-Disposition when file is sent456 var headers = {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 457why, one step back457 'Content-Disposition': contentDisposition(name || path)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 458–459why, one step back458 };459Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 460–474why, one step back460 // merge user-provided headers461 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 = headers474Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7
line 475why, one step back475 // Resolve the full path for sendFileMerge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 476–478why, one step back476 var fullPath = !opts.root477 ? resolve(path)478 : pathFix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7
line 479why, one step back479Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 480–481why, one step back480 // send file481 return this.sendFile(fullPath, opts, done)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 482–484why, one step back482};483484/**Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 485why, one step back485 * Set _Content-Type_ response header with `type` through `mime.contentType()`tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 486why, one step back486 * 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 back487 * When no mapping is found though `mime.contentType()`, the type is set to488 * "application/octet-stream".tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 489–499why, one step back489 *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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 500why, one step back500 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 501–503why, one step back501 */502503res.contentType =Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 504–505why, one step back504res.type = function contentType(type) {505 var ct = type.indexOf('/') === -1Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 506why, one step back506 ? (mime.contentType(type) || 'application/octet-stream')Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 507–509why, one step back507 : type;508509 return this.set('Content-Type', ct);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 510–535why, one step back510};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 back536 * 'application/json': function () {tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 537–565why, one step back537 * 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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 566why, one step back566 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 567–572why, one step back567 */568569res.format = function(obj){570 var req = this.req;571 var next = req.next;572Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 573–574why, one step back573 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 back575Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 576–578why, one step back576 var key = keys.length > 0577 ? req.accepts(keys)578 : false;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 579–584why, one step back579580 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 back585 } else if (obj.default) {586 obj.default(req, this, next)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 587why, one step back587 } else {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 588–590why, one step back588 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 back591 }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 back601 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 602–603why, one step back602 */603Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
lines 604–608why, one step back604res.attachment = function attachment(filename) {605 if (filename) {606 this.type(extname(filename));607 }608tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 609why, one step back609 this.set('Content-Disposition', contentDisposition(filename));Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 610why, one step back610tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 611–614why, one step back611 return this;612};613614/**Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 615–625why, one step back615 * 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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 626why, one step back626 * @publicMerge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 627–636why, one step back627 */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 back637 : [prev, val]Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 638–643why, one step back638 }639640 return this.set(field, value);641};642643/**tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 644–654why, one step back644 * Set header `field` to `val`, or pass645 * 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 back655 * When the set header is "Content-Type", the type is expanded to include656 * the charset if not present using `mime.contentType()`.657 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 658–659why, one step back658 * @param {String|Object} field659 * @param {String|Array} valtests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 660why, one step back660 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 661why, one step back661 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 662–666why, one step back662 */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 back667 var value = Array.isArray(val)668 ? val.map(String)669 : String(val);670671 // add charset to content-typeMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
lines 672–675why, one step back672 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 back676 value = mime.contentType(value)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 677why, one step back677 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 678–679why, one step back678679 this.setHeader(field, value);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 680–692why, one step back680 } 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 back693 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 694–703why, one step back694 */695696res.get = function(field){697 return this.getHeader(field);698};699700/**701 * Clear cookie `name`.702 *703 * @param {String} nameMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 704why, one step back704 * @param {Object} [options]tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 705why, one step back705 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 706why, one step back706 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 707–708why, one step back707 */708Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 709why, one step back709res.clearCookie = function clearCookie(name, options) {Ignore `expires` and `maxAge` in `res.clearCookie()` (#5792)Jon Churchbefore adoption82fc12a
lines 710–713why, one step back710 // Force cookie expiration by setting expires to the past711 const opts = { path: '/', ...options, expires: new Date(1)};712 // ensure maxAge is not passed713 delete opts.maxAgeMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 714–715why, one step back714715 return this.cookie(name, '', opts);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 716–718why, one step back716};717718/**Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 719why, one step back719 * Set cookie `name` to `value`, with the given `options`.tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 720–731why, one step back720 *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 back732 * // same as abovetests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 733–735why, one step back733 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })734 *735 * @param {String} nameMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 736why, one step back736 * @param {String|Object} valueMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 737why, one step back737 * @param {Object} [options]tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 738why, one step back738 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 739why, one step back739 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 740–741why, one step back740 */741Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 742–743why, one step back742res.cookie = function (name, value, options) {743 var opts = merge({}, options);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 744why, one step back744 var secret = this.req.secret;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 745–748why, one step back745 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 back749 }750Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 751–756why, one step back751 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 back757 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 758why, one step back758Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 759–765why, one step back759 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 back766 }767768 if (opts.path == null) {769 opts.path = '/';770 }771772 this.append('Set-Cookie', cookie.serialize(name, String(val), opts));773tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 774–790why, one step back774 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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 791why, one step back791 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 792–793why, one step back792 */793Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 794why, one step back794res.location = function location(url) {Merge branch '5.0' into 5.xWes Toddbefore adoption5e2345e
line 795why, one step back795 var loc;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 796–797why, one step back796797 // "back" is an alias for the referrerMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 798–799why, one step back798 if (url === 'back') {799 loc = this.req.get('Referrer') || '/';Merge branch '5.0' into 5.xWes Toddbefore adoption5e2345e
lines 800–801why, one step back800 } else {801 loc = String(url);Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 802why, one step back802 }tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 803why, one step back803Merge branch '5.0' into 5.xWes Toddbefore adoption5e2345e
lines 804–805why, one step back804 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 back806Merge branch '5.0' into 5.xWes Toddbefore adoption5e2345e
lines 807–811why, one step back807 // Only encode after host to avoid invalid encoding which can introduce808 // 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 back812};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 back829 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 830–838why, one step back830 */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 back839 status = arguments[0]840 address = arguments[1]tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 841–843why, one step back841 }842843 // Set location headerMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 844why, one step back844 address = this.location(address).get('Location');tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 845–848why, one step back845846 // 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 back849 body = statuses.message[status] + '. Redirecting to ' + addresstests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 850–852why, one step back850 },851852 html: function(){Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f
line 853why, one step back853 var u = escapeHtml(address);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 854why, one step back854 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 back855 },856857 default: function(){858 body = '';859 }860 });861862 // RespondThrow on invalid status codes (#4212)Jon Churchbefore adoption723b545
line 863why, one step back863 this.status(status);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 864–867why, one step back864 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 back868 } else {869 this.end(body);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 870–878why, one step back870 }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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 879why, one step back879 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 880–897why, one step back880 */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 back898 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 899–900why, one step back899 */900Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 901–904why, one step back901res.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 back905 var req = this.req;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 906why, one step back906 var self = this;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 907–908why, one step back907908 // support callback function as second argMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 909–911why, one step back909 if (typeof options === 'function') {910 done = options;911 opts = {};tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 912–914why, one step back912 }913914 // merge res.localsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 915why, one step back915 opts._locals = self.locals;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 916–917why, one step back916917 // default callback to respondMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 918why, one step back918 done = done || function (err, str) {tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 919–923why, one step back919 if (err) return req.next(err);920 self.send(str);921 };922923 // renderMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 924why, one step back924 app.render(view, opts, done);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 925why, one step back925};Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 926–929why, one step back926927// 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 back930 var streaming;Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 931why, one step back931Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 932–937why, one step back932 // request aborted933 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 back938 err.code = 'ECONNABORTED';Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 939–941why, one step back939 callback(err);940 }941Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 942–965why, one step back942 // directory943 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 }965Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 966–970why, one step back966 // file967 function onfile() {968 streaming = false;969 }970Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 971–972why, one step back971 // finished972 function onfinish(err) {Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 973why, one step back973 if (err && err.code === 'ECONNRESET') return onaborted();Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 974–977why, one step back974 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 back978 if (streaming !== false && !done) {979 onaborted();980 return;981 }982Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 983–984why, one step back983 if (done) return;984 done = true;Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 985why, one step back985 callback();Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 986–988why, one step back986 });987 }988Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 989–994why, one step back989 // streaming990 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 back995 file.on('end', onend);996 file.on('error', onerror);Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 997–998why, one step back997 file.on('file', onfile);998 file.on('stream', onstream);Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 999–1015why, one step back999 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 back1016}10171018/**Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1019–1022why, one step back1019 * Stringify JSON, like JSON.stringify, but v8 optimized, with the1020 * ability to escape characters that can trigger HTML sniffing.1021 *1022 * @param {*} valueMerge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 1023why, one step back1023 * @param {function} replacerMerge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1024–1026why, one step back1024 * @param {number} spaces1025 * @param {boolean} escape1026 * @returns {string}Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 1027–1029why, one step back1027 * @private1028 */1029Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 1030why, one step back1030function stringify (value, replacer, spaces, escape) {Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 1031–1032why, one step back1031 // v8 checks arguments.length for optimizing simple call1032 // https://bugs.chromium.org/p/v8/issues/detail?id=4730Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 1033why, one step back1033 var json = replacer || spacesMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 1034–1035why, one step back1034 ? JSON.stringify(value, replacer, spaces)1035 : JSON.stringify(value);Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 1036why, one step back1036Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 1037why, one step back1037 if (escape && typeof json === 'string') {Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1038–1045why, one step back1038 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 back1046 /* istanbul ignore next: unreachable default */Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1047–1053why, one step back1047 default:1048 return c1049 }1050 })1051 }10521053 return jsonInvoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 1054why, one step back1054}