response.js
This file
Path
lib/response.js
652 lines · 14.9 KiB · JavaScript
Last changed by
Changed: res.send() only set ETag when not previously setTJ Holowaychukbefore adoption1a3e40d
History of this fileTake it
The walk stopped after 200 commits: lines older than 374d159 point at it.
update dependenciesTj Holowaychukbefore adoption374d159
lines 1–3why, one step back12/*!3 * Express - responseupdate headersTJ Holowaychukbefore adoptiona376980
line 4why, one step back4 * Copyright(c) 2010-2012 TJ Holowaychuk <tj@vision-media.ca>update dependenciesTj Holowaychukbefore adoption374d159
lines 5–16why, one step back5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var fs = require('fs')13 , http = require('http')14 , path = require('path')15 , connect = require('connect')16 , utils = connect.utilsAdded array support. Closes #1053TJ Holowaychukbefore adoption298899d
lines 17–18why, one step back17 , normalizeType = require('./utils').normalizeType18 , normalizeTypes = require('./utils').normalizeTypesupdate dependenciesTj Holowaychukbefore adoption374d159
lines 19–20why, one step back19 , statusCodes = http.STATUS_CODES20 , send = connect.static.sendAdded Buffer ETag support to res.send()TJ Holowaychukbefore adoptionc0a68fc
line 21why, one step back21 , crc = require('crc')update dependenciesTj Holowaychukbefore adoption374d159
lines 22–23why, one step back22 , mime = require('mime')23 , basename = path.basenameChanged to allow extname or type/subtype only. Closes #1072TJ Holowaychukbefore adoption24a4e95
line 24why, one step back24 , extname = path.extnameupdate dependenciesTj Holowaychukbefore adoption374d159
lines 25–66why, one step back25 , join = path.join;2627/**28 * Response prototype.29 */3031var res = module.exports = {32 __proto__: http.ServerResponse.prototype33};3435/**36 * Set status `code`.37 *38 * @param {Number} code39 * @return {ServerResponse}40 * @api public41 */4243res.status = function(code){44 this.statusCode = code;45 return this;46};4748/**49 * Send a response.50 *51 * Examples:52 *53 * res.send(new Buffer('wahoo'));54 * res.send({ some: 'json' });55 * res.send('<p>some html</p>');56 * res.send(404, 'Sorry, cant find that');57 * res.send(404);58 *59 * @param {Mixed} body or status60 * @param {Mixed} body61 * @return {ServerResponse}62 * @api public63 */6465res.send = function(body){66 var req = this.reqAdded conditional-GET support to res.send()TJ Holowaychukbefore adoptiona58e3de
lines 67–68why, one step back67 , head = 'HEAD' == req.method68 , len;update dependenciesTj Holowaychukbefore adoption374d159
lines 69–74why, one step back6970 // allow status / body71 if (2 == arguments.length) {72 this.statusCode = body;73 body = arguments[1];74 }Merge pull request #1100 from coolony/masterTJ Holowaychukbefore adoption1ddf0c8
line 75why, one step back75 refactored res.send()TJ Holowaychukbefore adoptionb809041
lines 76–77why, one step back76 // convert string objects to primitives77 if (body instanceof String) body = body.toString();update dependenciesTj Holowaychukbefore adoption374d159
lines 78–81why, one step back7879 switch (typeof body) {80 // response status81 case 'number':.contentType -> .type() in coreTJ Holowaychukbefore adoption7c2e1ad
line 82why, one step back82 this.get('Content-Type') || this.type('txt');update dependenciesTj Holowaychukbefore adoption374d159
lines 83–87why, one step back83 this.statusCode = body;84 body = http.STATUS_CODES[body];85 break;86 // string defaulting to html87 case 'string':utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 88why, one step back88 if (!this.get('Content-Type')) {update dependenciesTj Holowaychukbefore adoption374d159
line 89why, one step back89 this.charset = this.charset || 'utf-8';.contentType -> .type() in coreTJ Holowaychukbefore adoption7c2e1ad
line 90why, one step back90 this.type('html');update dependenciesTj Holowaychukbefore adoption374d159
lines 91–97why, one step back91 }92 break;93 case 'boolean':94 case 'object':95 if (null == body) {96 body = '';97 } else if (Buffer.isBuffer(body)) {.contentType -> .type() in coreTJ Holowaychukbefore adoption7c2e1ad
line 98why, one step back98 this.get('Content-Type') || this.type('bin');update dependenciesTj Holowaychukbefore adoption374d159
lines 99–105why, one step back99 } else {100 return this.json(body);101 }102 break;103 }104105 // populate Content-Lengthutilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 106why, one step back106 if (undefined !== body && !this.get('Content-Length')) {Added Buffer ETag support to res.send()TJ Holowaychukbefore adoptionc0a68fc
lines 107–109why, one step back107 this.set('Content-Length', len = Buffer.isBuffer(body)108 ? body.length109 : Buffer.byteLength(body));Added conditional-GET support to res.send()TJ Holowaychukbefore adoptiona58e3de
lines 110–113why, one step back110 }111112 // ETag support113 // TODO: W/ supportAdded Buffer ETag support to res.send()TJ Holowaychukbefore adoptionc0a68fc
line 114why, one step back114 if (len > 1024) {Changed: res.send() only set ETag when not previously setTJ Holowaychukbefore adoption1a3e40d
line 115why, one step back115 if (!this.get('ETag')) this.set('ETag', Buffer.isBuffer(body)Added Buffer ETag support to res.send()TJ Holowaychukbefore adoptionc0a68fc
lines 116–117why, one step back116 ? crc.buffer.crc32(body)117 : crc.crc32(body));Added conditional-GET support to res.send()TJ Holowaychukbefore adoptiona58e3de
line 118why, one step back118 if (req.fresh) this.statusCode = 304;update dependenciesTj Holowaychukbefore adoption374d159
lines 119–155why, one step back119 }120121 // strip irrelevant headers122 if (204 == this.statusCode || 304 == this.statusCode) {123 this.removeHeader('Content-Type');124 this.removeHeader('Content-Length');125 body = '';126 }127128 // respond129 this.end(head ? null : body);130 return this;131};132133/**134 * Send JSON response.135 *136 * Examples:137 *138 * res.json(null);139 * res.json({ user: 'tj' });140 * res.json(500, 'oh noes!');141 * res.json(404, 'I dont have that');142 *143 * @param {Mixed} obj or status144 * @param {Mixed} obj145 * @return {ServerResponse}146 * @api public147 */148149res.json = function(obj){150 // allow status / body151 if (2 == arguments.length) {152 this.statusCode = obj;153 obj = arguments[1];154 }155Fixed: use app.get() for all core settingsTJ Holowaychukbefore adoption8f87400
lines 156–159why, one step back156 var app = this.app157 , jsonp = app.get('jsonp callback')158 , replacer = app.get('json replacer')159 , spaces = app.get('json spaces')Added "json replacer" and "json spaces" settings. Closes #996 [credit to jed]TJ Holowaychukbefore adoptionff92afa
lines 160–161why, one step back160 , body = JSON.stringify(obj, replacer, spaces)161 , callback = this.req.query.callback;update dependenciesTj Holowaychukbefore adoption374d159
lines 162–163why, one step back162163 this.charset = this.charset || 'utf-8';utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 164why, one step back164 this.set('Content-Type', 'application/json');update dependenciesTj Holowaychukbefore adoption374d159
lines 165–166why, one step back165166 if (callback && jsonp) {utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 167why, one step back167 this.set('Content-Type', 'text/javascript');update dependenciesTj Holowaychukbefore adoption374d159
lines 168–187why, one step back168 body = callback.replace(/[^\w$.]/g, '') + '(' + body + ');';169 }170171 return this.send(body);172};173174/**175 * Transfer the file at the given `path`.176 * 177 * Automatically sets the _Content-Type_ response header field.178 * The callback `fn(err)` is invoked when the transfer is complete179 * or when an error occurs. Be sure to check `res.sentHeader`180 * if you wish to attempt responding, as the header and some data181 * may have already been transferred.182 *183 * Options:184 *185 * - `maxAge` defaulting to 0186 * - `root` root directory for relative filenames187 *Added new dox API docsTJ Holowaychukbefore adoption7e81cad
lines 188–207why, one step back188 * Examples:189 *190 * The following example illustrates how `res.sendfile()` may191 * be used as an alternative for the `static()` middleware for192 * dynamic situations. The code backing `res.sendfile()` is actually193 * the same code, so HTTP cache support etc is identical.194 *195 * app.get('/user/:uid/photos/:file', function(req, res){196 * var uid = req.params.uid197 * , file = req.params.file;198 * 199 * req.user.mayViewFilesFrom(uid, function(yes){200 * if (yes) {201 * res.sendfile('/uploads/' + uid + '/' + file);202 * } else {203 * res.send(403, 'Sorry! you cant see that.');204 * }205 * });206 * });207 *update dependenciesTj Holowaychukbefore adoption374d159
lines 208–229why, one step back208 * @param {String} path209 * @param {Object|Function} options or fn210 * @param {Function} fn211 * @api public212 */213214res.sendfile = function(path, options, fn){215 var self = this216 , req = self.req217 , next = this.req.next218 , options = options || {};219220 // support function as second arg221 if ('function' == typeof options) {222 fn = options;223 options = {};224 }225226 // callback227 options.callback = function(err){228 if (err) {229 // cast ENOENTrefactored res.sendfile()TJ Holowaychukbefore adoptioncf7d710
line 230why, one step back230 if ('ENOENT' == err.code) err = utils.error(404);update dependenciesTj Holowaychukbefore adoption374d159
lines 231–259why, one step back231232 // ditch content-disposition to prevent funky responses233 if (!self.headerSent) self.removeHeader('Content-Disposition');234235 // woot! callback available236 if (fn) return fn(err);237238 // lost in limbo if there's no callback239 if (self.headerSent) return;240241 return req.next(err);242 }243244 fn && fn();245 };246247 // transfer248 options.path = encodeURIComponent(path);249 send(this.req, this, next, options);250};251252/**253 * Transfer the file at the given `path` as an attachment.254 *255 * Optionally providing an alternate attachment `filename`,256 * and optional callback `fn(err)`. The callback is invoked257 * when the data transfer is complete, or when an error has258 * ocurred. Be sure to check `res.headerSent` if you plan to respond.259 *Added new dox API docsTJ Holowaychukbefore adoption7e81cad
lines 260–261why, one step back260 * This method uses `res.attachment()` and `res.sendfile()`.261 *update dependenciesTj Holowaychukbefore adoption374d159
lines 262–278why, one step back262 * @param {String} path263 * @param {String|Function} filename or fn264 * @param {Function} fn265 * @api public266 */267268res.download = function(path, filename, fn){269 // support function as second arg270 if ('function' == typeof filename) {271 fn = filename;272 filename = null;273 }274275 return this.attachment(filename || path).sendfile(path, fn);276};277278/**Changed to allow extname or type/subtype only. Closes #1072TJ Holowaychukbefore adoption24a4e95
lines 279–280why, one step back279 * Set _Content-Type_ response header with `type` through `mime.lookup()`280 * when it does not contain "/", or set the Content-Type to `type` otherwise.update dependenciesTj Holowaychukbefore adoption374d159
lines 281–283why, one step back281 *282 * Examples:283 *Changed to allow extname or type/subtype only. Closes #1072TJ Holowaychukbefore adoption24a4e95
lines 284–287why, one step back284 * res.type('.html');285 * res.type('html');286 * res.type('json');287 * res.type('application/json');update dependenciesTj Holowaychukbefore adoption374d159
lines 288–296why, one step back288 * res.type('png');289 *290 * @param {String} type291 * @return {ServerResponse} for chaining292 * @api public293 */294295res.contentType =296res.type = function(type){Changed to allow extname or type/subtype only. Closes #1072TJ Holowaychukbefore adoption24a4e95
lines 297–299why, one step back297 return this.set('Content-Type', ~type.indexOf('/')298 ? type299 : mime.lookup(type));Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
lines 300–312why, one step back300};301302/**303 * Respond to the Acceptable formats using an `obj`304 * of mime-type callbacks.305 *306 * This method uses `req.accepted`, an array of307 * acceptable types ordered by their quality values.308 * When "Accept" is not present the _first_ callback309 * is invoked, otherwise the first match is used. When310 * no match is performed the server responds with311 * 406 "Not Acceptable".312 *Added extname support to `res.format()`. Closes #1024TJ Holowaychukbefore adoptioneb3105e
lines 313–315why, one step back313 * Content-Type is set for you, however if you choose314 * you may alter this within the callback using `res.type()`315 * or `res.set('Content-Type', ...)`.Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
line 316why, one step back316 *renamed `res.respondTo()` to `res.format()`TJ Holowaychukbefore adoptionffcaa04
line 317why, one step back317 * res.format({Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
line 318why, one step back318 * 'text/plain': function(){Added extname support to `res.format()`. Closes #1024TJ Holowaychukbefore adoptioneb3105e
line 319why, one step back319 * res.send('hey');Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
lines 320–330why, one step back320 * },321 * 322 * 'text/html': function(){323 * res.send('<p>hey</p>');324 * },325 * 326 * 'appliation/json': function(){327 * res.send({ message: 'hey' });328 * }329 * });330 *Added extname support to `res.format()`. Closes #1024TJ Holowaychukbefore adoptioneb3105e
lines 331–347why, one step back331 * In addition to canonicalized MIME types you may332 * also use extnames mapped to these types:333 *334 * res.format({335 * text: function(){336 * res.send('hey');337 * },338 * 339 * html: function(){340 * res.send('<p>hey</p>');341 * },342 * 343 * json: function(){344 * res.send({ message: 'hey' });345 * }346 * });347 *Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
lines 348–352why, one step back348 * @param {Object} obj349 * @return {ServerResponse} for chaining350 * @api public351 */352renamed `res.respondTo()` to `res.format()`TJ Holowaychukbefore adoptionffcaa04
line 353why, one step back353res.format = function(obj){Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
lines 354–356why, one step back354 var keys = Object.keys(obj)355 , req = this.req356 , next = req.nextAdded array support. Closes #1053TJ Holowaychukbefore adoption298899d
line 357why, one step back357 , key = req.accepts(keys);Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
line 358why, one step back358Added: `res.format()` sets Vary: AcceptTJ Holowaychukbefore adoption12e0878
lines 359–360why, one step back359 this.set('Vary', 'Accept');360Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
line 361why, one step back361 if (key) {Added array support. Closes #1053TJ Holowaychukbefore adoption298899d
line 362why, one step back362 this.set('Content-Type', normalizeType(key));Added: `res.respondTo()` defaults the content-typeTJ Holowaychukbefore adoptionbdfa6d1
line 363why, one step back363 obj[key](req, this, next);Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
lines 364–366why, one step back364 } else {365 var err = new Error('Not Acceptable');366 err.status = 406;Added array support. Closes #1053TJ Holowaychukbefore adoption298899d
line 367why, one step back367 err.types = normalizeTypes(keys);Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
lines 368–371why, one step back368 next(err);369 }370371 return this;update dependenciesTj Holowaychukbefore adoption374d159
lines 372–382why, one step back372};373374/**375 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.376 *377 * @param {String} filename378 * @return {ServerResponse}379 * @api public380 */381382res.attachment = function(filename){Changed to allow extname or type/subtype only. Closes #1072TJ Holowaychukbefore adoption24a4e95
line 383why, one step back383 if (filename) this.type(extname(filename));utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 384why, one step back384 this.set('Content-Disposition', filenameupdate dependenciesTj Holowaychukbefore adoption374d159
lines 385–398why, one step back385 ? 'attachment; filename="' + basename(filename) + '"'386 : 'attachment');387 return this;388};389390/**391 * Set header `field` to `val`, or pass392 * an object of of header fields.393 *394 * Examples:395 *396 * res.set('Accept', 'application/json');397 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });398 *docsTJ Holowaychukbefore adoption27b1b5e
lines 399–400why, one step back399 * Aliased as `res.header()`. 400 *update dependenciesTj Holowaychukbefore adoption374d159
lines 401–406why, one step back401 * @param {String|Object} field402 * @param {String} val403 * @return {ServerResponse} for chaining404 * @api public405 */406Added `res.header()` and `req.header()` aliases for BCTJ Holowaychukbefore adoption5fa2f89
lines 407–408why, one step back407res.set = 408res.header = function(field, val){update dependenciesTj Holowaychukbefore adoption374d159
line 409why, one step back409 if (2 == arguments.length) {Added: coerce `res.set()` values to stringsTJ Holowaychukbefore adoption67ddb42
line 410why, one step back410 this.setHeader(field, '' + val);update dependenciesTj Holowaychukbefore adoption374d159
lines 411–412why, one step back411 } else {412 for (var key in field) {Added: coerce `res.set()` values to stringsTJ Holowaychukbefore adoption67ddb42
line 413why, one step back413 this.setHeader(key, '' + field[key]);update dependenciesTj Holowaychukbefore adoption374d159
lines 414–492why, one step back414 }415 }416 return this;417};418419/**420 * Get value for header `field`.421 *422 * @param {String} field423 * @return {String}424 * @api public425 */426427res.get = function(field){428 return this.getHeader(field);429};430431/**432 * Clear cookie `name`.433 *434 * @param {String} name435 * @param {Object} options436 * @param {ServerResponse} for chaining437 * @api public438 */439440res.clearCookie = function(name, options){441 var opts = { expires: new Date(1), path: '/' };442 return this.cookie(name, '', options443 ? utils.merge(opts, options)444 : opts);445};446447/**448 * Set a signed cookie with the given `name` and `val`.449 * See `res.cookie()` for details.450 *451 * @param {String} name452 * @param {String|Object} val453 * @param {Object} options454 * @api public455 */456457res.signedCookie = function(name, val, options){458 var secret = this.req.secret;459 if (!secret) throw new Error('connect.cookieParser("secret") required for signed cookies');460 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);461 val = utils.sign(val, secret);462 return this.cookie(name, val, options);463};464465/**466 * Set cookie `name` to `val`, with the given `options`.467 *468 * Options:469 *470 * - `maxAge` max-age in milliseconds, converted to `expires`471 * - `path` defaults to "/"472 *473 * Examples:474 *475 * // "Remember Me" for 15 minutes476 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });477 *478 * // save as above479 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })480 *481 * @param {String} name482 * @param {String|Object} val483 * @param {Options} options484 * @api public485 */486487res.cookie = function(name, val, options){488 options = options || {};489 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);490 if ('maxAge' in options) options.expires = new Date(Date.now() + options.maxAge);491 if (null == options.path) options.path = '/';492 var cookie = utils.serializeCookie(name, val, options);utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 493why, one step back493 this.set('Set-Cookie', cookie);update dependenciesTj Holowaychukbefore adoption374d159
lines 494–509why, one step back494 return this;495};496497/**498 * Redirect to the given `url` with optional response `status`499 * defaulting to 302.500 *501 * The given `url` can also be the name of a mapped url, for502 * example by default express supports "back" which redirects503 * to the _Referrer_ or _Referer_ headers or "/".504 *505 * Examples:506 *507 * res.redirect('/foo/bar');508 * res.redirect('http://example.com');509 * res.redirect(301, 'http://example.com');added another res.redirect() exampleTJ Holowaychukbefore adoptionba413ee
line 510why, one step back510 * res.redirect('../login'); // /blog/post/1 -> /blog/loginupdate dependenciesTj Holowaychukbefore adoption374d159
lines 511–543why, one step back511 *512 * Mounting:513 *514 * When an application is mounted, and `res.redirect()`515 * is given a path that does _not_ lead with "/". For 516 * example suppose a "blog" app is mounted at "/blog",517 * the following redirect would result in "/blog/login":518 *519 * res.redirect('login');520 *521 * While the leading slash would result in a redirect to "/login":522 *523 * res.redirect('/login');524 *525 * @param {String} url526 * @param {Number} code527 * @api public528 */529530res.redirect = function(url){531 var app = this.app532 , req = this.req533 , head = 'HEAD' == req.method534 , status = 302535 , body;536537 // allow status / url538 if (2 == arguments.length) {539 status = url;540 url = arguments[1];541 }542543 // setup redirect mapRemoved `res.header(field, value)`TJ Holowaychukbefore adoption76fd462
line 544why, one step back544 var map = { back: req.get('Referrer') || '/' };update dependenciesTj Holowaychukbefore adoption374d159
lines 545–561why, one step back545546 // perform redirect547 url = map[url] || url;548549 // relative550 if (!~url.indexOf('://')) {551 var path = app.path();552553 // relative to path554 if (0 == url.indexOf('./') || 0 == url.indexOf('..')) {555 url = req.path + '/' + url;556 // relative to mount-point557 } else if ('/' != url[0]) {558 url = path + '/' + url;559 }560561 // AbsoluteRemoved `res.header(field, value)`TJ Holowaychukbefore adoption76fd462
line 562why, one step back562 var host = req.get('Host');Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
line 563why, one step back563 url = req.protocol + '://' + host + url;update dependenciesTj Holowaychukbefore adoption374d159
lines 564–566why, one step back564 }565566 // Support text/{plain,html} by defaultrenamed `res.respondTo()` to `res.format()`TJ Holowaychukbefore adoptionffcaa04
line 567why, one step back567 this.format({Added new dox API docsTJ Holowaychukbefore adoption7e81cad
line 568why, one step back568 text: function(){refactored res.redirect() with respondTo()TJ Holowaychukbefore adoptiona47660b
lines 569–571why, one step back569 body = statusCodes[status] + '. Redirecting to ' + url;570 },571Added new dox API docsTJ Holowaychukbefore adoption7e81cad
line 572why, one step back572 html: function(){refactored res.redirect() with respondTo()TJ Holowaychukbefore adoptiona47660b
lines 573–575why, one step back573 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';574 }575 })update dependenciesTj Holowaychukbefore adoption374d159
lines 576–578why, one step back576577 // Respond578 this.statusCode = status;utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 579why, one step back579 this.set('Location', url);update dependenciesTj Holowaychukbefore adoption374d159
lines 580–652why, one step back580 this.end(head ? null : body);581};582583/**584 * Render `view` with the given `options` and optional callback `fn`.585 * When a callback function is given a response will _not_ be made586 * automatically, otherwise a response of _200_ and _text/html_ is given.587 *588 * Options:589 * 590 * - `status` Response status code (`res.statusCode`)591 * - `charset` Set the charset (`res.charset`)592 *593 * Reserved locals:594 *595 * - `cache` boolean hinting to the engine it should cache596 * - `filename` filename of the view being rendered597 *598 * @param {String} view599 * @param {Object|Function} options or callback function600 * @param {Function} fn601 * @api public602 */603604res.render = function(view, options, fn){605 var self = this606 , options = options || {}607 , req = this.req608 , app = req.app;609610 // support callback function as second arg611 if ('function' == typeof options) {612 fn = options, options = {};613 }614615 function render() {616 // merge res.locals617 options.locals = self.locals;618619 // default callback to respond620 fn = fn || function(err, str){621 if (err) return req.next(err);622 self.send(str);623 };624625 // render626 app.render(view, options, fn);627 }628629 // invoke view callbacks630 var callbacks = app.viewCallbacks631 , pending = callbacks.length632 , len = pending633 , done;634635 if (len) {636 for (var i = 0; i < len; ++i) {637 callbacks[i](req, self, function(err){638 if (done) return;639640 if (err) {641 req.next(err);642 done = true;643 return;644 }645646 --pending || render();647 });648 }649 } else {650 render();651 }652};