Skip to content
Myr

response.js

This file

Path

lib/response.js

778 lines · 17.7 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

Release 3.2.4TJ Holowaychukbefore adoptionf976625

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

move setHeader charset patch to .setJonathan Ongbefore adoptionbad55f7

lines 5–18why, one step back
5var http = require('http');
6var path = require('path');7var mixin = require('utils-merge');8var escapeHtml = require('escape-html');9var sign = require('cookie-signature').sign;10var normalizeType = require('./utils').normalizeType;11var normalizeTypes = require('./utils').normalizeTypes;12var etag = require('./utils').etag;13var statusCodes = http.STATUS_CODES;14var cookie = require('cookie');15var send = require('send');16var basename = path.basename;17var extname = path.extname;18var mime = send.mime;

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 19–56why, one step back
19
20/**21 * Response prototype.22 */2324var res = module.exports = {25 __proto__: http.ServerResponse.prototype26};2728/**29 * Set status `code`.30 *31 * @param {Number} code32 * @return {ServerResponse}33 * @api public34 */3536res.status = function(code){37 this.statusCode = code;38 return this;39};4041/**42 * Set Link header field with the given `links`.43 *44 * Examples:45 *46 * res.links({47 * next: 'http://api.example.com/users?page=2',48 * last: 'http://api.example.com/users?page=5'49 * });50 *51 * @param {Object} links52 * @return {ServerResponse}53 * @api public54 */5556res.links = function(links){

refactor res.links()TJ Holowaychukbefore adoption8e2f538

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

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 60–81why, one step back
60 return '<' + links[rel] + '>; rel="' + rel + '"';
61 }).join(', '));62};6364/**65 * Send a response.66 *67 * Examples:68 *69 * res.send(new Buffer('wahoo'));70 * res.send({ some: 'json' });71 * res.send('<p>some html</p>');72 * res.send(404, 'Sorry, cant find that');73 * res.send(404);74 *75 * @param {Mixed} body or status76 * @param {Mixed} body77 * @return {ServerResponse}78 * @api public79 */8081res.send = function(body){

change generation of ETags with res.send() to GET requests only. Closes #1619TJ Holowaychukbefore adoption3296ed9

lines 82–84why, one step back
82 var req = this.req;
83 var head = 'HEAD' == req.method;84 var len;

Release 3.2.4TJ Holowaychukbefore adoptionf976625

line 85why, one step back
85

Merge pull request #1694 from kavu/add_disable_etagTJ Holowaychukbefore adoption610e172

lines 86–88why, one step back
86 // settings
87 var app = this.app;88

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 89–108why, one step back
89 // allow status / body
90 if (2 == arguments.length) {91 // res.send(body, status) backwards compat92 if ('number' != typeof body && 'number' == typeof arguments[1]) {93 this.statusCode = arguments[1];94 } else {95 this.statusCode = body;96 body = arguments[1];97 }98 }99100 switch (typeof body) {101 // response status102 case 'number':103 this.get('Content-Type') || this.type('txt');104 this.statusCode = body;105 body = http.STATUS_CODES[body];106 break;107 // string defaulting to html108 case 'string':

remove res.charset usageJonathan Ongbefore adoption2064f41

line 109why, one step back
109 if (!this.get('Content-Type')) this.type('html');

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 110–131why, one step back
110 break;
111 case 'boolean':112 case 'object':113 if (null == body) {114 body = '';115 } else if (Buffer.isBuffer(body)) {116 this.get('Content-Type') || this.type('bin');117 } else {118 return this.json(body);119 }120 break;121 }122123 // populate Content-Length124 if (undefined !== body && !this.get('Content-Length')) {125 this.set('Content-Length', len = Buffer.isBuffer(body)126 ? body.length127 : Buffer.byteLength(body));128 }129130 // ETag support131 // TODO: W/ support

always send ETag when content-length > 0Jonathan Ongbefore adoption55d1a4f

line 132why, one step back
132 if (app.settings.etag && len && 'GET' == req.method) {

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 133–235why, one step back
133 if (!this.get('ETag')) {
134 this.set('ETag', etag(body));135 }136 }137138 // freshness139 if (req.fresh) this.statusCode = 304;140141 // strip irrelevant headers142 if (204 == this.statusCode || 304 == this.statusCode) {143 this.removeHeader('Content-Type');144 this.removeHeader('Content-Length');145 this.removeHeader('Transfer-Encoding');146 body = '';147 }148149 // respond150 this.end(head ? null : body);151 return this;152};153154/**155 * Send JSON response.156 *157 * Examples:158 *159 * res.json(null);160 * res.json({ user: 'tj' });161 * res.json(500, 'oh noes!');162 * res.json(404, 'I dont have that');163 *164 * @param {Mixed} obj or status165 * @param {Mixed} obj166 * @return {ServerResponse}167 * @api public168 */169170res.json = function(obj){171 // allow status / body172 if (2 == arguments.length) {173 // res.json(body, status) backwards compat174 if ('number' == typeof arguments[1]) {175 this.statusCode = arguments[1];176 } else {177 this.statusCode = obj;178 obj = arguments[1];179 }180 }181182 // settings183 var app = this.app;184 var replacer = app.get('json replacer');185 var spaces = app.get('json spaces');186 var body = JSON.stringify(obj, replacer, spaces);187188 // content-type189 this.get('Content-Type') || this.set('Content-Type', 'application/json');190191 return this.send(body);192};193194/**195 * Send JSON response with JSONP callback support.196 *197 * Examples:198 *199 * res.jsonp(null);200 * res.jsonp({ user: 'tj' });201 * res.jsonp(500, 'oh noes!');202 * res.jsonp(404, 'I dont have that');203 *204 * @param {Mixed} obj or status205 * @param {Mixed} obj206 * @return {ServerResponse}207 * @api public208 */209210res.jsonp = function(obj){211 // allow status / body212 if (2 == arguments.length) {213 // res.json(body, status) backwards compat214 if ('number' == typeof arguments[1]) {215 this.statusCode = arguments[1];216 } else {217 this.statusCode = obj;218 obj = arguments[1];219 }220 }221222 // settings223 var app = this.app;224 var replacer = app.get('json replacer');225 var spaces = app.get('json spaces');226 var body = JSON.stringify(obj, replacer, spaces)227 .replace(/\u2028/g, '\\u2028')228 .replace(/\u2029/g, '\\u2029');229 var callback = this.req.query[app.get('jsonp callback name')];230231 // content-type232 this.set('Content-Type', 'application/json');233234 // jsonp235 if (callback) {

Merge pull request #1711 from jonjenkins/masterTJ Holowaychukbefore adoption3ea7381

line 236why, one step back
236 if (Array.isArray(callback)) callback = callback[0];

Release 3.2.4TJ Holowaychukbefore adoptionf976625

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

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

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

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 240–285why, one step back
240 }
241242 return this.send(body);243};244245/**246 * Transfer the file at the given `path`.247 *248 * Automatically sets the _Content-Type_ response header field.249 * The callback `fn(err)` is invoked when the transfer is complete250 * or when an error occurs. Be sure to check `res.sentHeader`251 * if you wish to attempt responding, as the header and some data252 * may have already been transferred.253 *254 * Options:255 *256 * - `maxAge` defaulting to 0257 * - `root` root directory for relative filenames258 *259 * Examples:260 *261 * The following example illustrates how `res.sendfile()` may262 * be used as an alternative for the `static()` middleware for263 * dynamic situations. The code backing `res.sendfile()` is actually264 * the same code, so HTTP cache support etc is identical.265 *266 * app.get('/user/:uid/photos/:file', function(req, res){267 * var uid = req.params.uid268 * , file = req.params.file;269 *270 * req.user.mayViewFilesFrom(uid, function(yes){271 * if (yes) {272 * res.sendfile('/uploads/' + uid + '/' + file);273 * } else {274 * res.send(403, 'Sorry! you cant see that.');275 * }276 * });277 * });278 *279 * @param {String} path280 * @param {Object|Function} options or fn281 * @param {Function} fn282 * @api public283 */284285res.sendfile = function(path, options, fn){

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

lines 286–290why, one step back
286 options = options || {};
287 var self = this;288 var req = self.req;289 var next = this.req.next;290 var done;

Release 3.2.4TJ Holowaychukbefore adoptionf976625

line 291why, one step back
291

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

line 292why, one step back
292

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 293–308why, one step back
293 // support function as second arg
294 if ('function' == typeof options) {295 fn = options;296 options = {};297 }298299 // socket errors300 req.socket.on('error', error);301302 // errors303 function error(err) {304 if (done) return;305 done = true;306307 // clean up308 cleanup();

remove ServerResonse.headerSent monkey patchRoman Shtylmanbefore adoption6835289

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

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 310–314why, one step back
310
311 // callback available312 if (fn) return fn(err);313314 // list in limbo if there's no callback

remove ServerResonse.headerSent monkey patchRoman Shtylmanbefore adoption6835289

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

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 316–321why, one step back
316
317 // delegate318 next(err);319 }320321 // streaming

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

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

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 323–324why, one step back
323 if (done) return;
324 cleanup();

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

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

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 326–349why, one step back
326 }
327328 // cleanup329 function cleanup() {330 req.socket.removeListener('error', error);331 }332333 // transfer334 var file = send(req, path);335 if (options.root) file.root(options.root);336 file.maxage(options.maxAge || 0);337 file.on('error', error);338 file.on('directory', next);339 file.on('stream', stream);340 file.pipe(this);341 this.on('finish', cleanup);342};343344/**345 * Transfer the file at the given `path` as an attachment.346 *347 * Optionally providing an alternate attachment `filename`,348 * and optional callback `fn(err)`. The callback is invoked349 * when the data transfer is complete, or when an error has

remove ServerResonse.headerSent monkey patchRoman Shtylmanbefore adoption6835289

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

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 351–453why, one step back
351 *
352 * This method uses `res.sendfile()`.353 *354 * @param {String} path355 * @param {String|Function} filename or fn356 * @param {Function} fn357 * @api public358 */359360res.download = function(path, filename, fn){361 // support function as second arg362 if ('function' == typeof filename) {363 fn = filename;364 filename = null;365 }366367 filename = filename || path;368 this.set('Content-Disposition', 'attachment; filename="' + basename(filename) + '"');369 return this.sendfile(path, fn);370};371372/**373 * Set _Content-Type_ response header with `type` through `mime.lookup()`374 * when it does not contain "/", or set the Content-Type to `type` otherwise.375 *376 * Examples:377 *378 * res.type('.html');379 * res.type('html');380 * res.type('json');381 * res.type('application/json');382 * res.type('png');383 *384 * @param {String} type385 * @return {ServerResponse} for chaining386 * @api public387 */388389res.contentType =390res.type = function(type){391 return this.set('Content-Type', ~type.indexOf('/')392 ? type393 : mime.lookup(type));394};395396/**397 * Respond to the Acceptable formats using an `obj`398 * of mime-type callbacks.399 *400 * This method uses `req.accepted`, an array of401 * acceptable types ordered by their quality values.402 * When "Accept" is not present the _first_ callback403 * is invoked, otherwise the first match is used. When404 * no match is performed the server responds with405 * 406 "Not Acceptable".406 *407 * Content-Type is set for you, however if you choose408 * you may alter this within the callback using `res.type()`409 * or `res.set('Content-Type', ...)`.410 *411 * res.format({412 * 'text/plain': function(){413 * res.send('hey');414 * },415 *416 * 'text/html': function(){417 * res.send('<p>hey</p>');418 * },419 *420 * 'appliation/json': function(){421 * res.send({ message: 'hey' });422 * }423 * });424 *425 * In addition to canonicalized MIME types you may426 * also use extnames mapped to these types:427 *428 * res.format({429 * text: function(){430 * res.send('hey');431 * },432 *433 * html: function(){434 * res.send('<p>hey</p>');435 * },436 *437 * json: function(){438 * res.send({ message: 'hey' });439 * }440 * });441 *442 * By default Express passes an `Error`443 * with a `.status` of 406 to `next(err)`444 * if a match is not made. If you provide445 * a `.default` callback it will be invoked446 * instead.447 *448 * @param {Object} obj449 * @return {ServerResponse} for chaining450 * @api public451 */452453res.format = function(obj){

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

lines 454–455why, one step back
454 var req = this.req;
455 var next = req.next;

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 456–462why, one step back
456
457 var fn = obj.default;458 if (fn) delete obj.default;459 var keys = Object.keys(obj);460461 var key = req.accepts(keys);462

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

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

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 464–465why, one step back
464
465 if (key) {

remove res.charset usageJonathan Ongbefore adoption2064f41

line 466why, one step back
466 this.set('Content-Type', normalizeType(key).value);

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 467–518why, one step back
467 obj[key](req, this, next);
468 } else if (fn) {469 fn();470 } else {471 var err = new Error('Not Acceptable');472 err.status = 406;473 err.types = normalizeTypes(keys).map(function(o){ return o.value });474 next(err);475 }476477 return this;478};479480/**481 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.482 *483 * @param {String} filename484 * @return {ServerResponse}485 * @api public486 */487488res.attachment = function(filename){489 if (filename) this.type(extname(filename));490 this.set('Content-Disposition', filename491 ? 'attachment; filename="' + basename(filename) + '"'492 : 'attachment');493 return this;494};495496/**497 * Set header `field` to `val`, or pass498 * an object of header fields.499 *500 * Examples:501 *502 * res.set('Foo', ['bar', 'baz']);503 * res.set('Accept', 'application/json');504 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });505 *506 * Aliased as `res.header()`.507 *508 * @param {String|Object|Array} field509 * @param {String} val510 * @return {ServerResponse} for chaining511 * @api public512 */513514res.set =515res.header = function(field, val){516 if (2 == arguments.length) {517 if (Array.isArray(val)) val = val.map(String);518 else val = String(val);

move setHeader charset patch to .setJonathan Ongbefore adoptionbad55f7

lines 519–523why, one step back
519 field = field.toLowerCase();
520 if ('content-type' == field && !/;\s*charset\s*=/.test(val)) {521 var charset = mime.charsets.lookup(val.split(';')[0]);522 if (charset) val += '; charset=' + charset.toLowerCase();523 }

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 524–556why, one step back
524 this.setHeader(field, val);
525 } else {526 for (var key in field) {527 this.set(key, field[key]);528 }529 }530 return this;531};532533/**534 * Get value for header `field`.535 *536 * @param {String} field537 * @return {String}538 * @api public539 */540541res.get = function(field){542 return this.getHeader(field);543};544545/**546 * Clear cookie `name`.547 *548 * @param {String} name549 * @param {Object} options550 * @param {ServerResponse} for chaining551 * @api public552 */553554res.clearCookie = function(name, options){555 var opts = { expires: new Date(1), path: '/' };556 return this.cookie(name, '', options

use utils-merge module to mixin object propertiesRoman Shtylmanbefore adoption718e68f

line 557why, one step back
557 ? mixin(opts, options)

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 558–584why, one step back
558 : opts);
559};560561/**562 * Set cookie `name` to `val`, with the given `options`.563 *564 * Options:565 *566 * - `maxAge` max-age in milliseconds, converted to `expires`567 * - `signed` sign the cookie568 * - `path` defaults to "/"569 *570 * Examples:571 *572 * // "Remember Me" for 15 minutes573 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });574 *575 * // save as above576 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })577 *578 * @param {String} name579 * @param {String|Object} val580 * @param {Options} options581 * @api public582 */583584res.cookie = function(name, val, options){

use utils-merge module to mixin object propertiesRoman Shtylmanbefore adoption718e68f

line 585why, one step back
585 options = mixin({}, options);

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 586–587why, one step back
586 var secret = this.req.secret;
587 var signed = options.signed;

remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08

line 588why, one step back
588 if (signed && !secret) throw new Error('cookieParser("secret") required for signed cookies');

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 589–596why, one step back
589 if ('number' == typeof val) val = val.toString();
590 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);591 if (signed) val = 's:' + sign(val, secret);592 if ('maxAge' in options) {593 options.expires = new Date(Date.now() + options.maxAge);594 options.maxAge /= 1000;595 }596 if (null == options.path) options.path = '/';

move support for multiple `res.cookie` calls to lib/responseRoman Shtylmanbefore adoptiond42d8f5

lines 597–608why, one step back
597 var headerVal = cookie.serialize(name, String(val), options);
598599 // supports multiple 'res.cookie' calls by getting previous value600 var prev = this.get('Set-Cookie');601 if (prev) {602 if (Array.isArray(prev)) {603 headerVal = prev.concat(headerVal);604 } else {605 headerVal = [prev, headerVal];606 }607 }608 this.set('Set-Cookie', headerVal);

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 609–615why, one step back
609 return this;
610};611612613/**614 * Set the location header to `url`.615 *

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

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

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 617–622why, one step back
617 * to the _Referrer_ or _Referer_ headers or "/".
618 *619 * Examples:620 *621 * res.location('/foo/bar').;622 * res.location('http://example.com');

res.location: remove resolving relative urlsJonathan Ongbefore adoptionaac1d52

line 623why, one step back
623 * res.location('../login');

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 624–629why, one step back
624 *
625 * @param {String} url626 * @api public627 */628629res.location = function(url){

res.location: remove resolving relative urlsJonathan Ongbefore adoptionaac1d52

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

Release 3.2.4TJ Holowaychukbefore adoptionf976625

line 631why, one step back
631

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

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

ocdTJ Holowaychukbefore adoption8a7a695

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

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 634–661why, one step back
634
635 // Respond636 this.set('Location', url);637 return this;638};639640/**641 * Redirect to the given `url` with optional response `status`642 * defaulting to 302.643 *644 * The resulting `url` is determined by `res.location()`, so645 * it will play nicely with mounted apps, relative paths,646 * `"back"` etc.647 *648 * Examples:649 *650 * res.redirect('/foo/bar');651 * res.redirect('http://example.com');652 * res.redirect(301, 'http://example.com');653 * res.redirect('http://example.com', 301);654 * res.redirect('../login'); // /blog/post/1 -> /blog/login655 *656 * @param {String} url657 * @param {Number} code658 * @api public659 */660661res.redirect = function(url){

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

lines 662–664why, one step back
662 var head = 'HEAD' == this.req.method;
663 var status = 302;664 var body;

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 665–686why, one step back
665
666 // allow status / url667 if (2 == arguments.length) {668 if ('number' == typeof url) {669 status = url;670 url = arguments[1];671 } else {672 status = arguments[1];673 }674 }675676 // Set location header677 this.location(url);678 url = this.get('Location');679680 // Support text/{plain,html} by default681 this.format({682 text: function(){683 body = statusCodes[status] + '. Redirecting to ' + encodeURI(url);684 },685686 html: function(){

use escape-html module to escape htmlRoman Shtylmanbefore adoption63c6a9c

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

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 688–699why, one step back
688 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';
689 },690691 default: function(){692 body = '';693 }694 });695696 // Respond697 this.statusCode = status;698 this.set('Content-Length', Buffer.byteLength(body));699 this.end(head ? null : body);

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

lines 700–726why, one step back
700};
701702/**703 * Add `field` to Vary. If already present in the Vary set, then704 * this call is simply ignored.705 *706 * @param {Array|String} field707 * @param {ServerResponse} for chaining708 * @api public709 */710711res.vary = function(field){712 var self = this;713714 // nothing715 if (!field) return this;716717 // array718 if (Array.isArray(field)) {719 field.forEach(function(field){720 self.vary(field);721 });722 return;723 }724725 var vary = this.get('Vary');726

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

line 727why, one step back
727 // append

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

lines 728–737why, one step back
728 if (vary) {
729 vary = vary.split(/ *, */);730 if (!~vary.indexOf(field)) vary.push(field);731 this.set('Vary', vary.join(', '));732 return this;733 }734735 // set736 this.set('Vary', field);737 return this;

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 738–756why, one step back
738};
739740/**741 * Render `view` with the given `options` and optional callback `fn`.742 * When a callback function is given a response will _not_ be made743 * automatically, otherwise a response of _200_ and _text/html_ is given.744 *745 * Options:746 *747 * - `cache` boolean hinting to the engine it should cache748 * - `filename` filename of the view being rendered749 *750 * @param {String} view751 * @param {Object|Function} options or callback function752 * @param {Function} fn753 * @api public754 */755756res.render = function(view, options, fn){

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

lines 757–760why, one step back
757 options = options || {};
758 var self = this;759 var req = this.req;760 var app = req.app;

Release 3.2.4TJ Holowaychukbefore adoptionf976625

lines 761–778why, one step back
761
762 // support callback function as second arg763 if ('function' == typeof options) {764 fn = options, options = {};765 }766767 // merge res.locals768 options._locals = self.locals;769770 // default callback to respond771 fn = fn || function(err, str){772 if (err) return req.next(err);773 self.send(str);774 };775776 // render777 app.render(view, options, fn);778};