response.js
This file
Path
lib/response.js
646 lines · 14.8 KiB · JavaScript
Last changed by
Fixed: use app.get() for all core settingsTJ Holowaychukbefore adoption8f87400
History of this fileTake it
The walk stopped after 200 commits: lines older than b6611c8 point at it.
Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 1–16why, one step back12/*!3 * Express - response4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * 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').normalizeTypesAdded: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 19–22why, one step back19 , statusCodes = http.STATUS_CODES20 , send = connect.static.send21 , mime = require('mime')22 , basename = path.basenameChanged to allow extname or type/subtype only. Closes #1072TJ Holowaychukbefore adoption24a4e95
line 23why, one step back23 , extname = path.extnameAdded: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 24–72why, one step back24 , join = path.join;2526/**27 * Response prototype.28 */2930var res = module.exports = {31 __proto__: http.ServerResponse.prototype32};3334/**35 * Set status `code`.36 *37 * @param {Number} code38 * @return {ServerResponse}39 * @api public40 */4142res.status = function(code){43 this.statusCode = code;44 return this;45};4647/**48 * Send a response.49 *50 * Examples:51 *52 * res.send(new Buffer('wahoo'));53 * res.send({ some: 'json' });54 * res.send('<p>some html</p>');55 * res.send(404, 'Sorry, cant find that');56 * res.send(404);57 *58 * @param {Mixed} body or status59 * @param {Mixed} body60 * @return {ServerResponse}61 * @api public62 */6364res.send = function(body){65 var req = this.req66 , head = 'HEAD' == req.method;6768 // allow status / body69 if (2 == arguments.length) {70 this.statusCode = body;71 body = arguments[1];72 }Merge pull request #1100 from coolony/masterTJ Holowaychukbefore adoption1ddf0c8
line 73why, one step back73 refactored res.send()TJ Holowaychukbefore adoptionb809041
lines 74–75why, one step back74 // convert string objects to primitives75 if (body instanceof String) body = body.toString();Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 76–79why, one step back7677 switch (typeof body) {78 // response status79 case 'number':utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 80why, one step back80 this.get('Content-Type') || this.contentType('.txt');Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 81–85why, one step back81 this.statusCode = body;82 body = http.STATUS_CODES[body];83 break;84 // string defaulting to html85 case 'string':utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 86why, one step back86 if (!this.get('Content-Type')) {Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 87–95why, one step back87 this.charset = this.charset || 'utf-8';88 this.contentType('.html');89 }90 break;91 case 'boolean':92 case 'object':93 if (null == body) {94 body = '';95 } else if (Buffer.isBuffer(body)) {utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 96why, one step back96 this.get('Content-Type') || this.contentType('.bin');Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 97–103why, one step back97 } else {98 return this.json(body);99 }100 break;101 }102103 // populate Content-Lengthutilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
lines 104–105why, one step back104 if (undefined !== body && !this.get('Content-Length')) {105 this.set('Content-Length', Buffer.isBuffer(body)Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 106–144why, one step back106 ? body.length107 : Buffer.byteLength(body));108 }109110 // strip irrelevant headers111 if (204 == this.statusCode || 304 == this.statusCode) {112 this.removeHeader('Content-Type');113 this.removeHeader('Content-Length');114 body = '';115 }116117 // respond118 this.end(head ? null : body);119 return this;120};121122/**123 * Send JSON response.124 *125 * Examples:126 *127 * res.json(null);128 * res.json({ user: 'tj' });129 * res.json(500, 'oh noes!');130 * res.json(404, 'I dont have that');131 *132 * @param {Mixed} obj or status133 * @param {Mixed} obj134 * @return {ServerResponse}135 * @api public136 */137138res.json = function(obj){139 // allow status / body140 if (2 == arguments.length) {141 this.statusCode = obj;142 obj = arguments[1];143 }144Fixed: use app.get() for all core settingsTJ Holowaychukbefore adoption8f87400
lines 145–148why, one step back145 var app = this.app146 , jsonp = app.get('jsonp callback')147 , replacer = app.get('json replacer')148 , spaces = app.get('json spaces')Added "json replacer" and "json spaces" settings. Closes #996 [credit to jed]TJ Holowaychukbefore adoptionff92afa
lines 149–150why, one step back149 , body = JSON.stringify(obj, replacer, spaces)150 , callback = this.req.query.callback;Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 151–152why, one step back151152 this.charset = this.charset || 'utf-8';utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 153why, one step back153 this.set('Content-Type', 'application/json');Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 154–155why, one step back154155 if (callback && jsonp) {utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 156why, one step back156 this.set('Content-Type', 'text/javascript');Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 157–176why, one step back157 body = callback.replace(/[^\w$.]/g, '') + '(' + body + ');';158 }159160 return this.send(body);161};162163/**164 * Transfer the file at the given `path`.165 * 166 * Automatically sets the _Content-Type_ response header field.167 * The callback `fn(err)` is invoked when the transfer is complete168 * or when an error occurs. Be sure to check `res.sentHeader`169 * if you wish to attempt responding, as the header and some data170 * may have already been transferred.171 *172 * Options:173 *174 * - `maxAge` defaulting to 0175 * - `root` root directory for relative filenames176 *Added new dox API docsTJ Holowaychukbefore adoption7e81cad
lines 177–196why, one step back177 * Examples:178 *179 * The following example illustrates how `res.sendfile()` may180 * be used as an alternative for the `static()` middleware for181 * dynamic situations. The code backing `res.sendfile()` is actually182 * the same code, so HTTP cache support etc is identical.183 *184 * app.get('/user/:uid/photos/:file', function(req, res){185 * var uid = req.params.uid186 * , file = req.params.file;187 * 188 * req.user.mayViewFilesFrom(uid, function(yes){189 * if (yes) {190 * res.sendfile('/uploads/' + uid + '/' + file);191 * } else {192 * res.send(403, 'Sorry! you cant see that.');193 * }194 * });195 * });196 *Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 197–253why, one step back197 * @param {String} path198 * @param {Object|Function} options or fn199 * @param {Function} fn200 * @api public201 */202203res.sendfile = function(path, options, fn){204 var self = this205 , req = self.req206 , next = this.req.next207 , options = options || {};208209 // support function as second arg210 if ('function' == typeof options) {211 fn = options;212 options = {};213 }214215 // callback216 options.callback = function(err){217 if (err) {218 // cast ENOENT219 if ('ENOENT' == err.code) err = 404;220221 // coerce numeric error to an Error222 // TODO: remove223 // TODO: remove docs for headerSent?224 if ('number' == typeof err) err = utils.error(err);225226 // 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 back254 * This method uses `res.attachment()` and `res.sendfile()`.255 *Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 256–272why, one step back256 * @param {String} path257 * @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 back273 * Set _Content-Type_ response header with `type` through `mime.lookup()`274 * when it does not contain "/", or set the Content-Type to `type` otherwise.Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 275–277why, one step back275 *276 * Examples:277 *Changed to allow extname or type/subtype only. Closes #1072TJ Holowaychukbefore adoption24a4e95
lines 278–281why, one step back278 * res.type('.html');279 * res.type('html');280 * res.type('json');281 * res.type('application/json');Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 282–290why, one step back282 * 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 back291 return this.set('Content-Type', ~type.indexOf('/')292 ? type293 : mime.lookup(type));Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
lines 294–306why, one step back294};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 back307 * Content-Type is set for you, however if you choose308 * 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 back310 *renamed `res.respondTo()` to `res.format()`TJ Holowaychukbefore adoptionffcaa04
line 311why, one step back311 * res.format({Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
line 312why, one step back312 * 'text/plain': function(){Added extname support to `res.format()`. Closes #1024TJ Holowaychukbefore adoptioneb3105e
line 313why, one step back313 * res.send('hey');Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
lines 314–324why, one step back314 * },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 back325 * In addition to canonicalized MIME types you may326 * 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 back342 * @param {Object} obj343 * @return {ServerResponse} for chaining344 * @api public345 */346renamed `res.respondTo()` to `res.format()`TJ Holowaychukbefore adoptionffcaa04
line 347why, one step back347res.format = function(obj){Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
lines 348–350why, one step back348 var keys = Object.keys(obj)349 , req = this.req350 , next = req.nextAdded array support. Closes #1053TJ Holowaychukbefore adoption298899d
line 351why, one step back351 , key = req.accepts(keys);Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
line 352why, one step back352Added: `res.format()` sets Vary: AcceptTJ Holowaychukbefore adoption12e0878
lines 353–354why, one step back353 this.set('Vary', 'Accept');354Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
line 355why, one step back355 if (key) {Added array support. Closes #1053TJ Holowaychukbefore adoption298899d
line 356why, one step back356 this.set('Content-Type', normalizeType(key));Added: `res.respondTo()` defaults the content-typeTJ Holowaychukbefore adoptionbdfa6d1
line 357why, one step back357 obj[key](req, this, next);Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
lines 358–360why, one step back358 } else {359 var err = new Error('Not Acceptable');360 err.status = 406;Added array support. Closes #1053TJ Holowaychukbefore adoption298899d
line 361why, one step back361 err.types = normalizeTypes(keys);Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
lines 362–365why, one step back362 next(err);363 }364365 return this;Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 366–376why, one step back366};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 back377 if (filename) this.type(extname(filename));utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 378why, one step back378 this.set('Content-Disposition', filenameAdded: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 379–392why, one step back379 ? '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 back393 * Aliased as `res.header()`. 394 *Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 395–400why, one step back395 * @param {String|Object} field396 * @param {String} val397 * @return {ServerResponse} for chaining398 * @api public399 */400Added `res.header()` and `req.header()` aliases for BCTJ Holowaychukbefore adoption5fa2f89
lines 401–402why, one step back401res.set = 402res.header = function(field, val){Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 403–434why, one step back403 if (2 == arguments.length) {404 this.setHeader(field, val);405 } else {406 for (var key in field) {407 this.setHeader(key, field[key]);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){removed "root" settingTj Holowaychukbefore adoption87b991c
line 435why, one step back435 var opts = { expires: new Date(1), path: '/' };Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 436–464why, one step back436 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`removed "root" settingTj Holowaychukbefore adoption87b991c
line 465why, one step back465 * - `path` defaults to "/"Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 466–484why, one step back466 *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);Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption85ea5f6
line 485why, one step back485 if (null == options.path) options.path = '/';Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
line 486why, one step back486 var cookie = utils.serializeCookie(name, val, options);utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 487why, one step back487 this.set('Set-Cookie', cookie);Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 488–496why, one step back488 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 redirectsremoved "root" settingTj Holowaychukbefore adoption87b991c
line 497why, one step back497 * to the _Referrer_ or _Referer_ headers or "/".Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 498–503why, one step back498 *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 back504 * res.redirect('../login'); // /blog/post/1 -> /blog/loginAdded: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
line 505why, one step back505 *docsTj Holowaychukbefore adoption0f72ca8
lines 506–518why, one step back506 * 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 *Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 519–536why, one step back519 * @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 }536Added relative redirect support. Closes #920Tj Holowaychukbefore adoption43c8764
line 537why, one step back537 // setup redirect mapRemoved `res.header(field, value)`TJ Holowaychukbefore adoption76fd462
line 538why, one step back538 var map = { back: req.get('Referrer') || '/' };Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
line 539why, one step back539Added relative redirect support. Closes #920Tj Holowaychukbefore adoption43c8764
line 540why, one step back540 // perform redirectRemoved `app.redirect()`Tj Holowaychukbefore adoption4329528
line 541why, one step back541 url = map[url] || url;Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
line 542why, one step back542Added relative redirect support. Closes #920Tj Holowaychukbefore adoption43c8764
line 543why, one step back543 // relativeAdded: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
line 544why, one step back544 if (!~url.indexOf('://')) {more testsTj Holowaychukbefore adoptioncca5d7e
line 545why, one step back545 var path = app.path();use app.path() in res.redirect()Tj Holowaychukbefore adoptiona1e325a
line 546why, one step back546Added support for ./ in redirectsTj Holowaychukbefore adoption489265e
line 547why, one step back547 // relative to pathAdded support for .. in redirects as wellTj Holowaychukbefore adoption88f154f
lines 548–549why, one step back548 if (0 == url.indexOf('./') || 0 == url.indexOf('..')) {549 url = req.path + '/' + url;Added relative redirect support. Closes #920Tj Holowaychukbefore adoption43c8764
line 550why, one step back550 // relative to mount-pointAdded support for ./ in redirectsTj Holowaychukbefore adoption489265e
line 551why, one step back551 } else if ('/' != url[0]) {Added relative redirect support. Closes #920Tj Holowaychukbefore adoption43c8764
lines 552–553why, one step back552 url = path + '/' + url;553 }Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 554–555why, one step back554555 // AbsoluteRemoved `res.header(field, value)`TJ Holowaychukbefore adoption76fd462
line 556why, one step back556 var host = req.get('Host');Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
line 557why, one step back557 url = req.protocol + '://' + host + url;Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 558–560why, one step back558 }559560 // Support text/{plain,html} by defaultrenamed `res.respondTo()` to `res.format()`TJ Holowaychukbefore adoptionffcaa04
line 561why, one step back561 this.format({Added new dox API docsTJ Holowaychukbefore adoption7e81cad
line 562why, one step back562 text: function(){refactored res.redirect() with respondTo()TJ Holowaychukbefore adoptiona47660b
lines 563–565why, one step back563 body = statusCodes[status] + '. Redirecting to ' + url;564 },565Added new dox API docsTJ Holowaychukbefore adoption7e81cad
line 566why, one step back566 html: function(){refactored res.redirect() with respondTo()TJ Holowaychukbefore adoptiona47660b
lines 567–569why, one step back567 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';568 }569 })Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 570–572why, one step back570571 // Respond572 this.statusCode = status;utilize `res.set()` internallyTJ Holowaychukbefore adoptionc44f018
line 573why, one step back573 this.set('Location', url);Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
lines 574–646why, one step back574 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 callbacks624 var callbacks = app.viewCallbacks625 , pending = callbacks.length626 , 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};