Skip to content
Myr

response.js

This file

Path

lib/response.js

646 lines · 14.9 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 1–10why, 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.utils

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

lines 11–12why, one step back
11 , normalizeType = require('./utils').normalizeType
12 , normalizeTypes = require('./utils').normalizeTypes

docsTJ Holowaychukbefore adoption6ab61d0

lines 13–14why, one step back
13 , statusCodes = http.STATUS_CODES
14 , send = connect.static.send

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

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 16–17why, one step back
16 , mime = require('mime')
17 , basename = path.basename

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

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 19–60why, one step back
19 , join = path.join;
2021/**22 * Response prototype.23 */2425var res = module.exports = {26 __proto__: http.ServerResponse.prototype27};2829/**30 * Set status `code`.31 *32 * @param {Number} code33 * @return {ServerResponse}34 * @api public35 */3637res.status = function(code){38 this.statusCode = code;39 return this;40};4142/**43 * Send a response.44 *45 * Examples:46 *47 * res.send(new Buffer('wahoo'));48 * res.send({ some: 'json' });49 * res.send('<p>some html</p>');50 * res.send(404, 'Sorry, cant find that');51 * res.send(404);52 *53 * @param {Mixed} body or status54 * @param {Mixed} body55 * @return {ServerResponse}56 * @api public57 */5859res.send = function(body){60 var req = this.req

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

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 63–68why, one step back
63
64 // allow status / body65 if (2 == arguments.length) {66 this.statusCode = body;67 body = arguments[1];68 }

Merge pull request #1100 from coolony/masterTJ Holowaychukbefore adoption1ddf0c8

line 69why, one step back
69

refactored res.send()TJ Holowaychukbefore adoptionb809041

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 72–75why, one step back
72
73 switch (typeof body) {74 // response status75 case 'number':

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

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 77–81why, one step back
77 this.statusCode = body;
78 body = http.STATUS_CODES[body];79 break;80 // string defaulting to html81 case 'string':

utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018

line 82why, one step back
82 if (!this.get('Content-Type')) {

docsTJ Holowaychukbefore adoption6ab61d0

line 83why, one step back
83 this.charset = this.charset || 'utf-8';

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

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 85–91why, one step back
85 }
86 break;87 case 'boolean':88 case 'object':89 if (null == body) {90 body = '';91 } else if (Buffer.isBuffer(body)) {

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

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 93–99why, one step back
93 } else {
94 return this.json(body);95 }96 break;97 }9899 // populate Content-Length

utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018

line 100why, one step back
100 if (undefined !== body && !this.get('Content-Length')) {

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

lines 101–103why, one step back
101 this.set('Content-Length', len = Buffer.isBuffer(body)
102 ? body.length103 : Buffer.byteLength(body));

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

lines 104–107why, one step back
104 }
105106 // ETag support107 // TODO: W/ support

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

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

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

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

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

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

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

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 113–149why, one step back
113 }
114115 // strip irrelevant headers116 if (204 == this.statusCode || 304 == this.statusCode) {117 this.removeHeader('Content-Type');118 this.removeHeader('Content-Length');119 body = '';120 }121122 // respond123 this.end(head ? null : body);124 return this;125};126127/**128 * Send JSON response.129 *130 * Examples:131 *132 * res.json(null);133 * res.json({ user: 'tj' });134 * res.json(500, 'oh noes!');135 * res.json(404, 'I dont have that');136 *137 * @param {Mixed} obj or status138 * @param {Mixed} obj139 * @return {ServerResponse}140 * @api public141 */142143res.json = function(obj){144 // allow status / body145 if (2 == arguments.length) {146 this.statusCode = obj;147 obj = arguments[1];148 }149

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

lines 150–153why, one step back
150 var app = this.app
151 , jsonp = app.get('jsonp callback')152 , replacer = app.get('json replacer')153 , spaces = app.get('json spaces')

docsTJ Holowaychukbefore adoption6ab61d0

lines 154–157why, one step back
154 , body = JSON.stringify(obj, replacer, spaces)
155 , callback = this.req.query.callback;156157 this.charset = this.charset || 'utf-8';

utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018

line 158why, one step back
158 this.set('Content-Type', 'application/json');

docsTJ Holowaychukbefore adoption6ab61d0

lines 159–160why, one step back
159
160 if (callback && jsonp) {

utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018

line 161why, one step back
161 this.set('Content-Type', 'text/javascript');

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

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 163–181why, one step back
163 }
164165 return this.send(body);166};167168/**169 * Transfer the file at the given `path`.170 * 171 * Automatically sets the _Content-Type_ response header field.172 * The callback `fn(err)` is invoked when the transfer is complete173 * or when an error occurs. Be sure to check `res.sentHeader`174 * if you wish to attempt responding, as the header and some data175 * may have already been transferred.176 *177 * Options:178 *179 * - `maxAge` defaulting to 0180 * - `root` root directory for relative filenames181 *

Added new dox API docsTJ Holowaychukbefore adoption7e81cad

lines 182–201why, one step back
182 * Examples:
183 *184 * The following example illustrates how `res.sendfile()` may185 * be used as an alternative for the `static()` middleware for186 * dynamic situations. The code backing `res.sendfile()` is actually187 * the same code, so HTTP cache support etc is identical.188 *189 * app.get('/user/:uid/photos/:file', function(req, res){190 * var uid = req.params.uid191 * , file = req.params.file;192 * 193 * req.user.mayViewFilesFrom(uid, function(yes){194 * if (yes) {195 * res.sendfile('/uploads/' + uid + '/' + file);196 * } else {197 * res.send(403, 'Sorry! you cant see that.');198 * }199 * });200 * });201 *

docsTJ Holowaychukbefore adoption6ab61d0

lines 202–223why, one step back
202 * @param {String} path
203 * @param {Object|Function} options or fn204 * @param {Function} fn205 * @api public206 */207208res.sendfile = function(path, options, fn){209 var self = this210 , req = self.req211 , next = this.req.next212 , options = options || {};213214 // support function as second arg215 if ('function' == typeof options) {216 fn = options;217 options = {};218 }219220 // callback221 options.callback = function(err){222 if (err) {223 // cast ENOENT

refactored res.sendfile()TJ Holowaychukbefore adoptioncf7d710

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 225–253why, one step back
225
226 // ditch content-disposition to prevent funky responses227 if (!self.headerSent) self.removeHeader('Content-Disposition');228229 // woot! callback available230 if (fn) return fn(err);231232 // lost in limbo if there's no callback233 if (self.headerSent) return;234235 return req.next(err);236 }237238 fn && fn();239 };240241 // transfer242 options.path = encodeURIComponent(path);243 send(this.req, this, next, options);244};245246/**247 * Transfer the file at the given `path` as an attachment.248 *249 * Optionally providing an alternate attachment `filename`,250 * and optional callback `fn(err)`. The callback is invoked251 * when the data transfer is complete, or when an error has252 * ocurred. Be sure to check `res.headerSent` if you plan to respond.253 *

Added new dox API docsTJ Holowaychukbefore adoption7e81cad

lines 254–255why, one step back
254 * This method uses `res.attachment()` and `res.sendfile()`.
255 *

docsTJ Holowaychukbefore adoption6ab61d0

lines 256–272why, one step back
256 * @param {String} path
257 * @param {String|Function} filename or fn258 * @param {Function} fn259 * @api public260 */261262res.download = function(path, filename, fn){263 // support function as second arg264 if ('function' == typeof filename) {265 fn = filename;266 filename = null;267 }268269 return this.attachment(filename || path).sendfile(path, fn);270};271272/**

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

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 275–277why, one step back
275 *
276 * Examples:277 *

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

lines 278–281why, one step back
278 * res.type('.html');
279 * res.type('html');280 * res.type('json');281 * res.type('application/json');

docsTJ Holowaychukbefore adoption6ab61d0

lines 282–290why, one step back
282 * res.type('png');
283 *284 * @param {String} type285 * @return {ServerResponse} for chaining286 * @api public287 */288289res.contentType =290res.type = function(type){

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

lines 291–293why, one step back
291 return this.set('Content-Type', ~type.indexOf('/')
292 ? type293 : mime.lookup(type));

Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25

lines 294–306why, one step back
294};
295296/**297 * Respond to the Acceptable formats using an `obj`298 * of mime-type callbacks.299 *300 * This method uses `req.accepted`, an array of301 * acceptable types ordered by their quality values.302 * When "Accept" is not present the _first_ callback303 * is invoked, otherwise the first match is used. When304 * no match is performed the server responds with305 * 406 "Not Acceptable".306 *

Added extname support to `res.format()`. Closes #1024TJ Holowaychukbefore adoptioneb3105e

lines 307–309why, one step back
307 * Content-Type is set for you, however if you choose
308 * you may alter this within the callback using `res.type()`309 * or `res.set('Content-Type', ...)`.

Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25

line 310why, one step back
310 *

renamed `res.respondTo()` to `res.format()`TJ Holowaychukbefore adoptionffcaa04

line 311why, one step back
311 * res.format({

Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25

line 312why, one step back
312 * 'text/plain': function(){

Added extname support to `res.format()`. Closes #1024TJ Holowaychukbefore adoptioneb3105e

line 313why, one step back
313 * res.send('hey');

Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25

lines 314–324why, one step back
314 * },
315 * 316 * 'text/html': function(){317 * res.send('<p>hey</p>');318 * },319 * 320 * 'appliation/json': function(){321 * res.send({ message: 'hey' });322 * }323 * });324 *

Added extname support to `res.format()`. Closes #1024TJ Holowaychukbefore adoptioneb3105e

lines 325–341why, one step back
325 * In addition to canonicalized MIME types you may
326 * also use extnames mapped to these types:327 *328 * res.format({329 * text: function(){330 * res.send('hey');331 * },332 * 333 * html: function(){334 * res.send('<p>hey</p>');335 * },336 * 337 * json: function(){338 * res.send({ message: 'hey' });339 * }340 * });341 *

Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25

lines 342–346why, one step back
342 * @param {Object} obj
343 * @return {ServerResponse} for chaining344 * @api public345 */346

renamed `res.respondTo()` to `res.format()`TJ Holowaychukbefore adoptionffcaa04

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

Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25

lines 348–350why, one step back
348 var keys = Object.keys(obj)
349 , req = this.req350 , next = req.next

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

line 351why, one step back
351 , key = req.accepts(keys);

Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25

line 352why, one step back
352

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

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

Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25

line 355why, one step back
355 if (key) {

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

Added: `res.respondTo()` defaults the content-typeTJ Holowaychukbefore adoptionbdfa6d1

line 357why, one step back
357 obj[key](req, this, next);

Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25

lines 358–360why, one step back
358 } else {
359 var err = new Error('Not Acceptable');360 err.status = 406;

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

line 361why, one step back
361 err.types = normalizeTypes(keys);

Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25

lines 362–365why, one step back
362 next(err);
363 }364365 return this;

docsTJ Holowaychukbefore adoption6ab61d0

lines 366–376why, one step back
366};
367368/**369 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.370 *371 * @param {String} filename372 * @return {ServerResponse}373 * @api public374 */375376res.attachment = function(filename){

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

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

utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018

line 378why, one step back
378 this.set('Content-Disposition', filename

docsTJ Holowaychukbefore adoption6ab61d0

lines 379–392why, one step back
379 ? 'attachment; filename="' + basename(filename) + '"'
380 : 'attachment');381 return this;382};383384/**385 * Set header `field` to `val`, or pass386 * an object of of header fields.387 *388 * Examples:389 *390 * res.set('Accept', 'application/json');391 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });392 *

docsTJ Holowaychukbefore adoption27b1b5e

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 395–400why, one step back
395 * @param {String|Object} field
396 * @param {String} val397 * @return {ServerResponse} for chaining398 * @api public399 */400

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

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

docsTJ Holowaychukbefore adoption6ab61d0

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

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

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

docsTJ Holowaychukbefore adoption6ab61d0

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

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

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 408–486why, one step back
408 }
409 }410 return this;411};412413/**414 * Get value for header `field`.415 *416 * @param {String} field417 * @return {String}418 * @api public419 */420421res.get = function(field){422 return this.getHeader(field);423};424425/**426 * Clear cookie `name`.427 *428 * @param {String} name429 * @param {Object} options430 * @param {ServerResponse} for chaining431 * @api public432 */433434res.clearCookie = function(name, options){435 var opts = { expires: new Date(1), path: '/' };436 return this.cookie(name, '', options437 ? utils.merge(opts, options)438 : opts);439};440441/**442 * Set a signed cookie with the given `name` and `val`.443 * See `res.cookie()` for details.444 *445 * @param {String} name446 * @param {String|Object} val447 * @param {Object} options448 * @api public449 */450451res.signedCookie = function(name, val, options){452 var secret = this.req.secret;453 if (!secret) throw new Error('connect.cookieParser("secret") required for signed cookies');454 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);455 val = utils.sign(val, secret);456 return this.cookie(name, val, options);457};458459/**460 * Set cookie `name` to `val`, with the given `options`.461 *462 * Options:463 *464 * - `maxAge` max-age in milliseconds, converted to `expires`465 * - `path` defaults to "/"466 *467 * Examples:468 *469 * // "Remember Me" for 15 minutes470 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });471 *472 * // save as above473 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })474 *475 * @param {String} name476 * @param {String|Object} val477 * @param {Options} options478 * @api public479 */480481res.cookie = function(name, val, options){482 options = options || {};483 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);484 if ('maxAge' in options) options.expires = new Date(Date.now() + options.maxAge);485 if (null == options.path) options.path = '/';486 var cookie = utils.serializeCookie(name, val, options);

utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018

line 487why, one step back
487 this.set('Set-Cookie', cookie);

docsTJ Holowaychukbefore adoption6ab61d0

lines 488–503why, one step back
488 return this;
489};490491/**492 * Redirect to the given `url` with optional response `status`493 * defaulting to 302.494 *495 * The given `url` can also be the name of a mapped url, for496 * example by default express supports "back" which redirects497 * to the _Referrer_ or _Referer_ headers or "/".498 *499 * Examples:500 *501 * res.redirect('/foo/bar');502 * res.redirect('http://example.com');503 * res.redirect(301, 'http://example.com');

added another res.redirect() exampleTJ Holowaychukbefore adoptionba413ee

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 505–537why, one step back
505 *
506 * Mounting:507 *508 * When an application is mounted, and `res.redirect()`509 * is given a path that does _not_ lead with "/". For 510 * example suppose a "blog" app is mounted at "/blog",511 * the following redirect would result in "/blog/login":512 *513 * res.redirect('login');514 *515 * While the leading slash would result in a redirect to "/login":516 *517 * res.redirect('/login');518 *519 * @param {String} url520 * @param {Number} code521 * @api public522 */523524res.redirect = function(url){525 var app = this.app526 , req = this.req527 , head = 'HEAD' == req.method528 , status = 302529 , body;530531 // allow status / url532 if (2 == arguments.length) {533 status = url;534 url = arguments[1];535 }536537 // setup redirect map

Removed `res.header(field, value)`TJ Holowaychukbefore adoption76fd462

line 538why, one step back
538 var map = { back: req.get('Referrer') || '/' };

docsTJ Holowaychukbefore adoption6ab61d0

lines 539–555why, one step back
539
540 // perform redirect541 url = map[url] || url;542543 // relative544 if (!~url.indexOf('://')) {545 var path = app.path();546547 // relative to path548 if (0 == url.indexOf('./') || 0 == url.indexOf('..')) {549 url = req.path + '/' + url;550 // relative to mount-point551 } else if ('/' != url[0]) {552 url = path + '/' + url;553 }554555 // Absolute

Removed `res.header(field, value)`TJ Holowaychukbefore adoption76fd462

line 556why, one step back
556 var host = req.get('Host');

docsTJ Holowaychukbefore adoption6ab61d0

lines 557–560why, one step back
557 url = req.protocol + '://' + host + url;
558 }559560 // Support text/{plain,html} by default

renamed `res.respondTo()` to `res.format()`TJ Holowaychukbefore adoptionffcaa04

line 561why, one step back
561 this.format({

Added new dox API docsTJ Holowaychukbefore adoption7e81cad

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

refactored res.redirect() with respondTo()TJ Holowaychukbefore adoptiona47660b

lines 563–565why, one step back
563 body = statusCodes[status] + '. Redirecting to ' + url;
564 },565

Added new dox API docsTJ Holowaychukbefore adoption7e81cad

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

refactored res.redirect() with respondTo()TJ Holowaychukbefore adoptiona47660b

lines 567–569why, one step back
567 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';
568 }569 })

docsTJ Holowaychukbefore adoption6ab61d0

lines 570–572why, one step back
570
571 // Respond572 this.statusCode = status;

utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018

line 573why, one step back
573 this.set('Location', url);

docsTJ Holowaychukbefore adoption6ab61d0

lines 574–623why, one step back
574 this.end(head ? null : body);
575};576577/**578 * Render `view` with the given `options` and optional callback `fn`.579 * When a callback function is given a response will _not_ be made580 * automatically, otherwise a response of _200_ and _text/html_ is given.581 *582 * Options:583 * 584 * - `status` Response status code (`res.statusCode`)585 * - `charset` Set the charset (`res.charset`)586 *587 * Reserved locals:588 *589 * - `cache` boolean hinting to the engine it should cache590 * - `filename` filename of the view being rendered591 *592 * @param {String} view593 * @param {Object|Function} options or callback function594 * @param {Function} fn595 * @api public596 */597598res.render = function(view, options, fn){599 var self = this600 , options = options || {}601 , req = this.req602 , app = req.app;603604 // support callback function as second arg605 if ('function' == typeof options) {606 fn = options, options = {};607 }608609 function render() {610 // merge res.locals611 options.locals = self.locals;612613 // default callback to respond614 fn = fn || function(err, str){615 if (err) return req.next(err);616 self.send(str);617 };618619 // render620 app.render(view, options, fn);621 }622623 // invoke view callbacks

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

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

docsTJ Holowaychukbefore adoption6ab61d0

lines 625–646why, one step back
625 , pending = callbacks.length
626 , len = pending627 , done;628629 if (len) {630 for (var i = 0; i < len; ++i) {631 callbacks[i](req, self, function(err){632 if (done) return;633634 if (err) {635 req.next(err);636 done = true;637 return;638 }639640 --pending || render();641 });642 }643 } else {644 render();645 }646};