Skip to content
Myr

response.js

This file

Path

lib/response.js

681 lines · 15.6 KiB · JavaScript

Last changed by

Added `res.send(body, status)` support back for backwards compatTJ Holowaychukbefore adoption3c0c961

History of this file

Take it

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

ocdTJ Holowaychukbefore adoption1d48584

lines 1–14why, one step back
1
2/**3 * Module dependencies.4 */56var fs = require('fs')7 , http = require('http')8 , path = require('path')9 , connect = require('connect')10 , utils = connect.utils11 , normalizeType = require('./utils').normalizeType12 , normalizeTypes = require('./utils').normalizeTypes13 , statusCodes = http.STATUS_CODES14 , send = connect.static.send

fixing cookies for connect 2.3.1TJ Holowaychukbefore adoption02d4384

line 15why, one step back
15 , cookie = require('cookie')

Added Buffer ETag support to res.send()TJ Holowaychukbefore adoptionc0a68fc

line 16why, one step back
16 , crc = require('crc')

Fixed: expose connects mime module. Cloases #1165TJ Holowaychukbefore adoption8956731

line 17why, one step back
17 , mime = connect.mime

ocdTJ Holowaychukbefore adoption1d48584

line 18why, one step back
18 , basename = path.basename

Changed to allow extname or type/subtype only. Closes #1072TJ Holowaychukbefore adoption24a4e95

line 19why, one step back
19 , extname = path.extname

ocdTJ Holowaychukbefore adoption1d48584

lines 20–43why, one step back
20 , join = path.join;
2122/**23 * Response prototype.24 */2526var res = module.exports = {27 __proto__: http.ServerResponse.prototype28};2930/**31 * Set status `code`.32 *33 * @param {Number} code34 * @return {ServerResponse}35 * @api public36 */3738res.status = function(code){39 this.statusCode = code;40 return this;41};4243/**

Added `res.links(obj)`TJ Holowaychukbefore adoptione6129d8

lines 44–64why, one step back
44 * Set Link header field with the given `links`.
45 *46 * Examples:47 *48 * res.links({49 * next: 'http://api.example.com/users?page=2',50 * last: 'http://api.example.com/users?page=5'51 * });52 *53 * @param {Object} links54 * @return {ServerResponse}55 * @api public56 */5758res.links = function(links){59 return this.set('Link', Object.keys(links).map(function(rel){60 return '<' + links[rel] + '>; rel="' + rel + '"';61 }).join(', '));62};6364/**

ocdTJ Holowaychukbefore adoption1d48584

lines 65–82why, one step back
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){82 var req = this.req

Added conditional-GET support to res.send()TJ Holowaychukbefore adoptiona58e3de

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

ocdTJ Holowaychukbefore adoption1d48584

lines 85–87why, one step back
85
86 // allow status / body87 if (2 == arguments.length) {

Added `res.send(body, status)` support back for backwards compatTJ Holowaychukbefore adoption3c0c961

lines 88–94why, one step back
88 // res.send(body, status) backwards compat
89 if ('number' != typeof body && 'number' == typeof arguments[1]) {90 this.statusCode = arguments[1];91 } else {92 this.statusCode = body;93 body = arguments[1];94 }

ocdTJ Holowaychukbefore adoption1d48584

line 95why, one step back
95 }

Added `res.send(body, status)` support back for backwards compatTJ Holowaychukbefore adoption3c0c961

lines 96–97why, one step back
96
97

refactored res.send()TJ Holowaychukbefore adoptionb809041

lines 98–99why, one step back
98 // convert string objects to primitives
99 if (body instanceof String) body = body.toString();

ocdTJ Holowaychukbefore adoption1d48584

lines 100–103why, one step back
100
101 switch (typeof body) {102 // response status103 case 'number':

.contentType -> .type() in coreTJ Holowaychukbefore adoption7c2e1ad

line 104why, one step back
104 this.get('Content-Type') || this.type('txt');

ocdTJ Holowaychukbefore adoption1d48584

lines 105–111why, one step back
105 this.statusCode = body;
106 body = http.STATUS_CODES[body];107 break;108 // string defaulting to html109 case 'string':110 if (!this.get('Content-Type')) {111 this.charset = this.charset || 'utf-8';

.contentType -> .type() in coreTJ Holowaychukbefore adoption7c2e1ad

line 112why, one step back
112 this.type('html');

ocdTJ Holowaychukbefore adoption1d48584

lines 113–119why, one step back
113 }
114 break;115 case 'boolean':116 case 'object':117 if (null == body) {118 body = '';119 } else if (Buffer.isBuffer(body)) {

.contentType -> .type() in coreTJ Holowaychukbefore adoption7c2e1ad

line 120why, one step back
120 this.get('Content-Type') || this.type('bin');

ocdTJ Holowaychukbefore adoption1d48584

lines 121–128why, one step back
121 } else {
122 return this.json(body);123 }124 break;125 }126127 // populate Content-Length128 if (undefined !== body && !this.get('Content-Length')) {

Added Buffer ETag support to res.send()TJ Holowaychukbefore adoptionc0a68fc

lines 129–131why, one step back
129 this.set('Content-Length', len = Buffer.isBuffer(body)
130 ? body.length131 : Buffer.byteLength(body));

Added conditional-GET support to res.send()TJ Holowaychukbefore adoptiona58e3de

lines 132–135why, one step back
132 }
133134 // ETag support135 // TODO: W/ support

Added Buffer ETag support to res.send()TJ Holowaychukbefore adoptionc0a68fc

line 136why, one step back
136 if (len > 1024) {

Changed: res.send() only set ETag when not previously setTJ Holowaychukbefore adoption1a3e40d

line 137why, one step back
137 if (!this.get('ETag')) this.set('ETag', Buffer.isBuffer(body)

Added Buffer ETag support to res.send()TJ Holowaychukbefore adoptionc0a68fc

lines 138–139why, one step back
138 ? crc.buffer.crc32(body)
139 : crc.crc32(body));

ocdTJ Holowaychukbefore adoption1d48584

line 140why, one step back
140 }

Changed: res.send() always checks freshnessTJ Holowaychukbefore adoption640cf4c

lines 141–142why, one step back
141
142 // freshness

Added 2xx / 304 check to req.freshTJ Holowaychukbefore adoptiond073e0a

line 143why, one step back
143 if (req.fresh) this.statusCode = 304;

ocdTJ Holowaychukbefore adoption1d48584

lines 144–179why, one step back
144
145 // strip irrelevant headers146 if (204 == this.statusCode || 304 == this.statusCode) {147 this.removeHeader('Content-Type');148 this.removeHeader('Content-Length');149 body = '';150 }151152 // respond153 this.end(head ? null : body);154 return this;155};156157/**158 * Send JSON response.159 *160 * Examples:161 *162 * res.json(null);163 * res.json({ user: 'tj' });164 * res.json(500, 'oh noes!');165 * res.json(404, 'I dont have that');166 *167 * @param {Mixed} obj or status168 * @param {Mixed} obj169 * @return {ServerResponse}170 * @api public171 */172173res.json = function(obj){174 // allow status / body175 if (2 == arguments.length) {176 this.statusCode = obj;177 obj = arguments[1];178 }179

Fixed: use app.get() for all core settingsTJ Holowaychukbefore adoption8f87400

lines 180–183why, one step back
180 var app = this.app
181 , jsonp = app.get('jsonp callback')182 , replacer = app.get('json replacer')183 , spaces = app.get('json spaces')

ocdTJ Holowaychukbefore adoption1d48584

lines 184–191why, one step back
184 , body = JSON.stringify(obj, replacer, spaces)
185 , callback = this.req.query.callback;186187 this.charset = this.charset || 'utf-8';188 this.set('Content-Type', 'application/json');189190 if (callback && jsonp) {191 this.set('Content-Type', 'text/javascript');

Added: allow [] in jsonp callback. Closes #1128TJ Holowaychukbefore adoptiondf2584c

line 192why, one step back
192 body = callback.replace(/[^[]\w$.]/g, '') + '(' + body + ');';

ocdTJ Holowaychukbefore adoption1d48584

lines 193–211why, one step back
193 }
194195 return this.send(body);196};197198/**199 * Transfer the file at the given `path`.200 * 201 * Automatically sets the _Content-Type_ response header field.202 * The callback `fn(err)` is invoked when the transfer is complete203 * or when an error occurs. Be sure to check `res.sentHeader`204 * if you wish to attempt responding, as the header and some data205 * may have already been transferred.206 *207 * Options:208 *209 * - `maxAge` defaulting to 0210 * - `root` root directory for relative filenames211 *

Added new dox API docsTJ Holowaychukbefore adoption7e81cad

lines 212–231why, one step back
212 * Examples:
213 *214 * The following example illustrates how `res.sendfile()` may215 * be used as an alternative for the `static()` middleware for216 * dynamic situations. The code backing `res.sendfile()` is actually217 * the same code, so HTTP cache support etc is identical.218 *219 * app.get('/user/:uid/photos/:file', function(req, res){220 * var uid = req.params.uid221 * , file = req.params.file;222 * 223 * req.user.mayViewFilesFrom(uid, function(yes){224 * if (yes) {225 * res.sendfile('/uploads/' + uid + '/' + file);226 * } else {227 * res.send(403, 'Sorry! you cant see that.');228 * }229 * });230 * });231 *

ocdTJ Holowaychukbefore adoption1d48584

lines 232–253why, one step back
232 * @param {String} path
233 * @param {Object|Function} options or fn234 * @param {Function} fn235 * @api public236 */237238res.sendfile = function(path, options, fn){239 var self = this240 , req = self.req241 , next = this.req.next242 , options = options || {};243244 // support function as second arg245 if ('function' == typeof options) {246 fn = options;247 options = {};248 }249250 // callback251 options.callback = function(err){252 if (err) {253 // cast ENOENT

refactored res.sendfile()TJ Holowaychukbefore adoptioncf7d710

line 254why, one step back
254 if ('ENOENT' == err.code) err = utils.error(404);

ocdTJ Holowaychukbefore adoption1d48584

lines 255–283why, one step back
255
256 // ditch content-disposition to prevent funky responses257 if (!self.headerSent) self.removeHeader('Content-Disposition');258259 // woot! callback available260 if (fn) return fn(err);261262 // lost in limbo if there's no callback263 if (self.headerSent) return;264265 return req.next(err);266 }267268 fn && fn();269 };270271 // transfer272 options.path = encodeURIComponent(path);273 send(this.req, this, next, options);274};275276/**277 * Transfer the file at the given `path` as an attachment.278 *279 * Optionally providing an alternate attachment `filename`,280 * and optional callback `fn(err)`. The callback is invoked281 * when the data transfer is complete, or when an error has282 * ocurred. Be sure to check `res.headerSent` if you plan to respond.283 *

Added res.download() testsTJ Holowaychukbefore adoption5840b42

line 284why, one step back
284 * This method uses `res.sendfile()`.

Added new dox API docsTJ Holowaychukbefore adoption7e81cad

line 285why, one step back
285 *

ocdTJ Holowaychukbefore adoption1d48584

lines 286–298why, one step back
286 * @param {String} path
287 * @param {String|Function} filename or fn288 * @param {Function} fn289 * @api public290 */291292res.download = function(path, filename, fn){293 // support function as second arg294 if ('function' == typeof filename) {295 fn = filename;296 filename = null;297 }298

Added res.download() testsTJ Holowaychukbefore adoption5840b42

lines 299–301why, one step back
299 filename = filename || path;
300 this.set('Content-Disposition', 'attachment; filename="' + basename(filename) + '"');301 return this.sendfile(path, fn);

ocdTJ Holowaychukbefore adoption1d48584

lines 302–304why, one step back
302};
303304/**

Changed to allow extname or type/subtype only. Closes #1072TJ Holowaychukbefore adoption24a4e95

lines 305–306why, one step back
305 * Set _Content-Type_ response header with `type` through `mime.lookup()`
306 * when it does not contain "/", or set the Content-Type to `type` otherwise.

ocdTJ Holowaychukbefore adoption1d48584

lines 307–309why, one step back
307 *
308 * Examples:309 *

Changed to allow extname or type/subtype only. Closes #1072TJ Holowaychukbefore adoption24a4e95

lines 310–313why, one step back
310 * res.type('.html');
311 * res.type('html');312 * res.type('json');313 * res.type('application/json');

ocdTJ Holowaychukbefore adoption1d48584

lines 314–322why, one step back
314 * res.type('png');
315 *316 * @param {String} type317 * @return {ServerResponse} for chaining318 * @api public319 */320321res.contentType =322res.type = function(type){

Changed to allow extname or type/subtype only. Closes #1072TJ Holowaychukbefore adoption24a4e95

lines 323–325why, one step back
323 return this.set('Content-Type', ~type.indexOf('/')
324 ? type325 : mime.lookup(type));

ocdTJ Holowaychukbefore adoption1d48584

lines 326–373why, one step back
326};
327328/**329 * Respond to the Acceptable formats using an `obj`330 * of mime-type callbacks.331 *332 * This method uses `req.accepted`, an array of333 * acceptable types ordered by their quality values.334 * When "Accept" is not present the _first_ callback335 * is invoked, otherwise the first match is used. When336 * no match is performed the server responds with337 * 406 "Not Acceptable".338 *339 * Content-Type is set for you, however if you choose340 * you may alter this within the callback using `res.type()`341 * or `res.set('Content-Type', ...)`.342 *343 * res.format({344 * 'text/plain': function(){345 * res.send('hey');346 * },347 * 348 * 'text/html': function(){349 * res.send('<p>hey</p>');350 * },351 * 352 * 'appliation/json': function(){353 * res.send({ message: 'hey' });354 * }355 * });356 *357 * In addition to canonicalized MIME types you may358 * also use extnames mapped to these types:359 *360 * res.format({361 * text: function(){362 * res.send('hey');363 * },364 * 365 * html: function(){366 * res.send('<p>hey</p>');367 * },368 * 369 * json: function(){370 * res.send({ message: 'hey' });371 * }372 * });373 *

mention res.format() callbackTJ Holowaychukbefore adoptionfd42b5c

lines 374–375why, one step back
374 * By default Express passes an `Error`
375 * with a `.status` of 406 to `next(err)`

docsTJ Holowaychukbefore adoptiond2baf11

lines 376–378why, one step back
376 * if a match is not made. If you provide
377 * a `.default` callback it will be invoked378 * instead.

mention res.format() callbackTJ Holowaychukbefore adoptionfd42b5c

line 379why, one step back
379 *

ocdTJ Holowaychukbefore adoption1d48584

lines 380–384why, one step back
380 * @param {Object} obj
381 * @return {ServerResponse} for chaining382 * @api public383 */384

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

line 385why, one step back
385res.format = function(obj){

ocdTJ Holowaychukbefore adoption1d48584

lines 386–387why, one step back
386 var keys = Object.keys(obj)
387 , req = this.req

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

line 388why, one step back
388 , next = req.next;

ocdTJ Holowaychukbefore adoption1d48584

line 389why, one step back
389

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

lines 390–394why, one step back
390 var fn = obj.default;
391 if (fn) delete obj.default;392393 var key = req.accepts(keys);394

Added: `res.format()` sets Vary: AcceptTJ Holowaychukbefore adoption12e0878

lines 395–396why, one step back
395 this.set('Vary', 'Accept');
396

ocdTJ Holowaychukbefore adoption1d48584

lines 397–399why, one step back
397 if (key) {
398 this.set('Content-Type', normalizeType(key));399 obj[key](req, this, next);

Fixed res.redirect() 406. Closes #1154TJ Holowaychukbefore adoption1311f2a

lines 400–401why, one step back
400 } else if (fn) {
401 fn();

ocdTJ Holowaychukbefore adoption1d48584

lines 402–420why, one step back
402 } else {
403 var err = new Error('Not Acceptable');404 err.status = 406;405 err.types = normalizeTypes(keys);406 next(err);407 }408409 return this;410};411412/**413 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.414 *415 * @param {String} filename416 * @return {ServerResponse}417 * @api public418 */419420res.attachment = function(filename){

Changed to allow extname or type/subtype only. Closes #1072TJ Holowaychukbefore adoption24a4e95

line 421why, one step back
421 if (filename) this.type(extname(filename));

ocdTJ Holowaychukbefore adoption1d48584

lines 422–429why, one step back
422 this.set('Content-Disposition', filename
423 ? 'attachment; filename="' + basename(filename) + '"'424 : 'attachment');425 return this;426};427428/**429 * Set header `field` to `val`, or pass

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

line 430why, one step back
430 * an object of header fields.

ocdTJ Holowaychukbefore adoption1d48584

lines 431–436why, one step back
431 *
432 * Examples:433 *434 * res.set('Accept', 'application/json');435 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });436 *

docsTJ Holowaychukbefore adoption27b1b5e

lines 437–438why, one step back
437 * Aliased as `res.header()`.
438 *

ocdTJ Holowaychukbefore adoption1d48584

lines 439–444why, one step back
439 * @param {String|Object} field
440 * @param {String} val441 * @return {ServerResponse} for chaining442 * @api public443 */444

Added `res.header()` and `req.header()` aliases for BCTJ Holowaychukbefore adoption5fa2f89

lines 445–446why, one step back
445res.set =
446res.header = function(field, val){

ocdTJ Holowaychukbefore adoption1d48584

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

Added: coerce `res.set()` values to stringsTJ Holowaychukbefore adoption67ddb42

line 448why, one step back
448 this.setHeader(field, '' + val);

ocdTJ Holowaychukbefore adoption1d48584

lines 449–450why, one step back
449 } else {
450 for (var key in field) {

Added: coerce `res.set()` values to stringsTJ Holowaychukbefore adoption67ddb42

line 451why, one step back
451 this.setHeader(key, '' + field[key]);

ocdTJ Holowaychukbefore adoption1d48584

lines 452–490why, one step back
452 }
453 }454 return this;455};456457/**458 * Get value for header `field`.459 *460 * @param {String} field461 * @return {String}462 * @api public463 */464465res.get = function(field){466 return this.getHeader(field);467};468469/**470 * Clear cookie `name`.471 *472 * @param {String} name473 * @param {Object} options474 * @param {ServerResponse} for chaining475 * @api public476 */477478res.clearCookie = function(name, options){479 var opts = { expires: new Date(1), path: '/' };480 return this.cookie(name, '', options481 ? utils.merge(opts, options)482 : opts);483};484485/**486 * Set cookie `name` to `val`, with the given `options`.487 *488 * Options:489 *490 * - `maxAge` max-age in milliseconds, converted to `expires`

Added { signed: true } option to res.cookie()TJ Holowaychukbefore adoption1150a88

line 491why, one step back
491 * - `signed` sign the cookie

ocdTJ Holowaychukbefore adoption1d48584

lines 492–509why, one step back
492 * - `path` defaults to "/"
493 *494 * Examples:495 *496 * // "Remember Me" for 15 minutes497 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });498 *499 * // save as above500 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })501 *502 * @param {String} name503 * @param {String|Object} val504 * @param {Options} options505 * @api public506 */507508res.cookie = function(name, val, options){509 options = options || {};

Added { signed: true } option to res.cookie()TJ Holowaychukbefore adoption1150a88

lines 510–512why, one step back
510 var secret = this.req.secret;
511 var signed = options.signed;512 if (signed && !secret) throw new Error('connect.cookieParser("secret") required for signed cookies');

ocdTJ Holowaychukbefore adoption1d48584

line 513why, one step back
513 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);

Added { signed: true } option to res.cookie()TJ Holowaychukbefore adoption1150a88

line 514why, one step back
514 if (signed) val = utils.sign(val, secret);

ocdTJ Holowaychukbefore adoption1d48584

lines 515–516why, one step back
515 if ('maxAge' in options) options.expires = new Date(Date.now() + options.maxAge);
516 if (null == options.path) options.path = '/';

coerce res.cookie values to stringsTJ Holowaychukbefore adoptionebf60d2

line 517why, one step back
517 this.set('Set-Cookie', cookie.serialize(name, String(val), options));

ocdTJ Holowaychukbefore adoption1d48584

lines 518–533why, one step back
518 return this;
519};520521/**522 * Redirect to the given `url` with optional response `status`523 * defaulting to 302.524 *525 * The given `url` can also be the name of a mapped url, for526 * example by default express supports "back" which redirects527 * to the _Referrer_ or _Referer_ headers or "/".528 *529 * Examples:530 *531 * res.redirect('/foo/bar');532 * res.redirect('http://example.com');533 * res.redirect(301, 'http://example.com');

added another res.redirect() exampleTJ Holowaychukbefore adoptionba413ee

line 534why, one step back
534 * res.redirect('../login'); // /blog/post/1 -> /blog/login

ocdTJ Holowaychukbefore adoption1d48584

lines 535–577why, one step back
535 *
536 * Mounting:537 *538 * When an application is mounted, and `res.redirect()`539 * is given a path that does _not_ lead with "/". For 540 * example suppose a "blog" app is mounted at "/blog",541 * the following redirect would result in "/blog/login":542 *543 * res.redirect('login');544 *545 * While the leading slash would result in a redirect to "/login":546 *547 * res.redirect('/login');548 *549 * @param {String} url550 * @param {Number} code551 * @api public552 */553554res.redirect = function(url){555 var app = this.app556 , req = this.req557 , head = 'HEAD' == req.method558 , status = 302559 , body;560561 // allow status / url562 if (2 == arguments.length) {563 status = url;564 url = arguments[1];565 }566567 // setup redirect map568 var map = { back: req.get('Referrer') || '/' };569570 // perform redirect571 url = map[url] || url;572573 // relative574 if (!~url.indexOf('://')) {575 var path = app.path();576577 // relative to path

refactor res.redirect() relative checkTJ Holowaychukbefore adoption362c96c

line 578why, one step back
578 if ('.' == url[0]) {

ocdTJ Holowaychukbefore adoption1d48584

lines 579–591why, one step back
579 url = req.path + '/' + url;
580 // relative to mount-point581 } else if ('/' != url[0]) {582 url = path + '/' + url;583 }584585 // Absolute586 var host = req.get('Host');587 url = req.protocol + '://' + host + url;588 }589590 // Support text/{plain,html} by default591 this.format({

Added new dox API docsTJ Holowaychukbefore adoption7e81cad

line 592why, one step back
592 text: function(){

ocdTJ Holowaychukbefore adoption1d48584

lines 593–595why, one step back
593 body = statusCodes[status] + '. Redirecting to ' + url;
594 },595

Added new dox API docsTJ Holowaychukbefore adoption7e81cad

line 596why, one step back
596 html: function(){

ocdTJ Holowaychukbefore adoption1d48584

line 597why, one step back
597 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

lines 598–601why, one step back
598 },
599600 default: function(){601 body = '';

ocdTJ Holowaychukbefore adoption1d48584

line 602why, one step back
602 }

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

line 603why, one step back
603 });

ocdTJ Holowaychukbefore adoption1d48584

lines 604–607why, one step back
604
605 // Respond606 this.statusCode = status;607 this.set('Location', url);

Fixed res.redirect() 406. Closes #1154TJ Holowaychukbefore adoption1311f2a

line 608why, one step back
608 this.set('Content-Length', Buffer.byteLength(body));

ocdTJ Holowaychukbefore adoption1d48584

lines 609–658why, one step back
609 this.end(head ? null : body);
610};611612/**613 * Render `view` with the given `options` and optional callback `fn`.614 * When a callback function is given a response will _not_ be made615 * automatically, otherwise a response of _200_ and _text/html_ is given.616 *617 * Options:618 * 619 * - `status` Response status code (`res.statusCode`)620 * - `charset` Set the charset (`res.charset`)621 *622 * Reserved locals:623 *624 * - `cache` boolean hinting to the engine it should cache625 * - `filename` filename of the view being rendered626 *627 * @param {String} view628 * @param {Object|Function} options or callback function629 * @param {Function} fn630 * @api public631 */632633res.render = function(view, options, fn){634 var self = this635 , options = options || {}636 , req = this.req637 , app = req.app;638639 // support callback function as second arg640 if ('function' == typeof options) {641 fn = options, options = {};642 }643644 function render() {645 // merge res.locals646 options.locals = self.locals;647648 // default callback to respond649 fn = fn || function(err, str){650 if (err) return req.next(err);651 self.send(str);652 };653654 // render655 app.render(view, options, fn);656 }657658 // invoke view callbacks

Added res.locals.use(). Closes #1120TJ Holowaychukbefore adoptiondd33ef2

line 659why, one step back
659 var callbacks = app.viewCallbacks.concat(self.viewCallbacks)

ocdTJ Holowaychukbefore adoption1d48584

lines 660–681why, one step back
660 , pending = callbacks.length
661 , len = pending662 , done;663664 if (len) {665 for (var i = 0; i < len; ++i) {666 callbacks[i](req, self, function(err){667 if (done) return;668669 if (err) {670 req.next(err);671 done = true;672 return;673 }674675 --pending || render();676 });677 }678 } else {679 render();680 }681};