response.js
This file
Path
lib/response.js
550 lines · 12.7 KiB · JavaScript
Last changed by
Added replacement for dynamic locals. Closes #788Tj Holowaychukbefore adoption73f1da2
History of this fileTake it
The walk stopped after 200 commits: lines older than e4342a7 point at it.
Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
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.utilsrefactored `res.redirect()` slightlyTj Holowaychukbefore adoption6bd5182
line 17why, one step back17 , statusCodes = http.STATUS_CODESAdded error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 18–24why, one step back18 , res = http.ServerResponse.prototype19 , send = connect.static.send20 , mime = require('mime')21 , basename = path.basename22 , join = path.join;2324/**Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
line 25why, one step back25 * Send a response.Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 26–31why, one step back26 *27 * Examples:28 *29 * res.send(new Buffer('wahoo'));30 * res.send({ some: 'json' });31 * res.send('<p>some html</p>');Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
line 32why, one step back32 * res.send(404, 'Sorry, cant find that');Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 33–34why, one step back33 * res.send(404);34 *Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
lines 35–36why, one step back35 * @param {Mixed} body or status36 * @param {Mixed} bodyAdded error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 37–40why, one step back37 * @return {ServerResponse}38 * @api public39 */40Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
line 41why, one step back41res.send = function(body){refactored `res.send()`Tj Holowaychukbefore adoption2c734e2
lines 42–44why, one step back42 var req = this.req43 , head = 'HEAD' == req.method;44Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
lines 45–49why, one step back45 // allow status / body46 if (2 == arguments.length) {47 this.statusCode = body;48 body = arguments[1];49 }Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 50–51why, one step back5051 switch (typeof body) {Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
line 52why, one step back52 // response statusAdded error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
line 53why, one step back53 case 'number':Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
lines 54–56why, one step back54 this.header('Content-Type') || this.contentType('.txt');55 this.statusCode = body;56 body = http.STATUS_CODES[body];Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
line 57why, one step back57 break;Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
line 58why, one step back58 // string defaulting to htmlAdded error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 59–66why, one step back59 case 'string':60 if (!this.header('Content-Type')) {61 this.charset = this.charset || 'utf-8';62 this.contentType('.html');63 }64 break;65 case 'boolean':66 case 'object':Changed `res.send(null)` responds with empty stringTj Holowaychukbefore adoption376bedf
lines 67–69why, one step back67 if (null == body) {68 body = '';69 } else if (Buffer.isBuffer(body)) {Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
line 70why, one step back70 this.header('Content-Type') || this.contentType('.bin');Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
line 71why, one step back71 } else {Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
line 72why, one step back72 return this.json(body);Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 73–77why, one step back73 }74 break;75 }7677 // populate Content-LengthFixed [23]04 supportTj Holowaychukbefore adoption5ac5352
line 78why, one step back78 if (undefined !== body && !this.header('Content-Length')) {Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 79–84why, one step back79 this.header('Content-Length', Buffer.isBuffer(body)80 ? body.length81 : Buffer.byteLength(body));82 }8384 // strip irrelevant headersChanged `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
line 85why, one step back85 if (204 == this.statusCode || 304 == this.statusCode) {Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 86–87why, one step back86 this.removeHeader('Content-Type');87 this.removeHeader('Content-Length');Fixed 204 / 304 responsesTj Holowaychukbefore adoptionc631eb4
line 88why, one step back88 body = '';Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 89–91why, one step back89 }9091 // respondrefactored `res.send()`Tj Holowaychukbefore adoption2c734e2
line 92why, one step back92 this.end(head ? null : body);Merge branch 'feature/res-json'Tj Holowaychukbefore adoption9e4020e
lines 93–96why, one step back93 return this;94};9596/**Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
line 97why, one step back97 * Send JSON response.Merge branch 'feature/res-json'Tj Holowaychukbefore adoption9e4020e
lines 98–102why, one step back98 *99 * Examples:100 *101 * res.json(null);102 * res.json({ user: 'tj' });Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
lines 103–104why, one step back103 * res.json(500, 'oh noes!');104 * res.json(404, 'I dont have that');Merge branch 'feature/res-json'Tj Holowaychukbefore adoption9e4020e
line 105why, one step back105 *Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
line 106why, one step back106 * @param {Mixed} obj or statusMerge branch 'feature/res-json'Tj Holowaychukbefore adoption9e4020e
lines 107–111why, one step back107 * @param {Mixed} obj108 * @return {ServerResponse}109 * @api public110 */111Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
lines 112–118why, one step back112res.json = function(obj){113 // allow status / body114 if (2 == arguments.length) {115 this.statusCode = obj;116 obj = arguments[1];117 }118Added `res.json()` JSONP support. Closes #737Tj Holowaychukbefore adoption73ea5cd
lines 119–122why, one step back119 var body = JSON.stringify(obj)120 , callback = this.req.query.callback121 , jsonp = this.app.enabled('jsonp callback');122Merge branch 'feature/res-json'Tj Holowaychukbefore adoption9e4020e
lines 123–124why, one step back123 this.charset = this.charset || 'utf-8';124 this.header('Content-Type', 'application/json');Added `res.json()` JSONP support. Closes #737Tj Holowaychukbefore adoption73ea5cd
lines 125–127why, one step back125126 if (callback && jsonp) {127 this.header('Content-Type', 'text/javascript');Revert "removed jsonp stripping"Tj Holowaychukbefore adoption60d16ea
line 128why, one step back128 body = callback.replace(/[^\w$.]/g, '') + '(' + body + ');';Added `res.json()` JSONP support. Closes #737Tj Holowaychukbefore adoption73ea5cd
lines 129–130why, one step back129 }130Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f
line 131why, one step back131 return this.send(body);Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 132–133why, one step back132};133docs for res.status()Tj Holowaychukbefore adoption9477c9b
lines 134–141why, one step back134/**135 * Set status `code`.136 *137 * @param {Number} code138 * @return {ServerResponse}139 * @api public140 */141Added chainable res.status(code)Tj Holowaychukbefore adoptionb04be51
lines 142–146why, one step back142res.status = function(code){143 this.statusCode = code;144 return this;145};146Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
line 147why, one step back147/**refactored `res.sendfile()`Tj Holowaychukbefore adoption5da28be
lines 148–151why, one step back148 * Transfer the file at the given `path`.149 * 150 * Automatically sets the _Content-Type_ response header field.151 * The callback `fn(err)` is invoked when the transfer is completemake `res.sendfile()` more like `res.download()`. Closes #774Tj Holowaychukbefore adoption7a476fc
line 152why, one step back152 * or when an error occurs. Be sure to check `res.sentHeader`refactored `res.sendfile()`Tj Holowaychukbefore adoption5da28be
lines 153–154why, one step back153 * if you wish to attempt responding, as the header and some data154 * may have already been transferred.Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 155–167why, one step back155 *156 * Options:157 *158 * - `maxAge` defaulting to 0159 * - `root` root directory for relative filenames160 *161 * @param {String} path162 * @param {Object|Function} options or fn163 * @param {Function} fn164 * @api public165 */166167res.sendfile = function(path, options, fn){refactored `res.sendfile()`Tj Holowaychukbefore adoption5da28be
line 168why, one step back168 var self = thisfixed `res.sendfile()` 404 supportTj Holowaychukbefore adoptiond2df055
line 169why, one step back169 , req = self.reqrefactored `res.sendfile()`Tj Holowaychukbefore adoption5da28be
lines 170–171why, one step back170 , next = this.req.next171 , options = options || {};Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 172–178why, one step back172173 // support function as second arg174 if ('function' == typeof options) {175 fn = options;176 options = {};177 }178refactored `res.sendfile()`Tj Holowaychukbefore adoption5da28be
lines 179–181why, one step back179 // callback180 options.callback = function(err){181 // TODO: remove when node adds thismake `res.sendfile()` more like `res.download()`. Closes #774Tj Holowaychukbefore adoption7a476fc
lines 182–188why, one step back182 self.sentHeader = !! self._header;183184 // error185 if (err) {186 // ditch content-disposition to prevent funky responses187 if (!self.sentHeader) self.removeHeader('Content-Disposition');188fixed `res.sendfile()` 404 supportTj Holowaychukbefore adoptiond2df055
lines 189–191why, one step back189 // not found190 if ('ENOENT' == err.code) return req.next();191make `res.sendfile()` more like `res.download()`. Closes #774Tj Holowaychukbefore adoption7a476fc
lines 192–197why, one step back192 // lost in limbo if there's no callback193 if (self.sentHeader) return fn && fn(err);194195 // callback available196 if (fn) return fn(err);197fixed `res.sendfile()` 404 supportTj Holowaychukbefore adoptiond2df055
line 198why, one step back198 return req.next(err);make `res.sendfile()` more like `res.download()`. Closes #774Tj Holowaychukbefore adoption7a476fc
lines 199–201why, one step back199 }200201 fn && fn();refactored `res.sendfile()`Tj Holowaychukbefore adoption5da28be
lines 202–204why, one step back202 };203204 // transferAdded error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 205–209why, one step back205 options.path = encodeURIComponent(path);206 send(this.req, this, next, options);207};208209/**build `res.download()` on top of `res.sendfile()`Tj Holowaychukbefore adoptiond3ccdbc
lines 210–233why, one step back210 * Transfer the file at the given `path` as an attachment.211 *212 * Optionally providing an alternate attachment `filename`,213 * and optional callback `fn(err)`. The callback is invoked214 * when the data transfer is complete, or when an error has215 * ocurred. Be sure to check `res.sendHeader` if you plan to respond.216 *217 * @param {String} path218 * @param {String|Function} filename or fn219 * @param {Function} fn220 * @api public221 */222223res.download = function(path, filename, fn){224 // support function as second arg225 if ('function' == typeof filename) {226 fn = filename;227 filename = null;228 }229230 return this.attachment(filename || path).sendfile(path, fn);231};232233/**Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 234–245why, one step back234 * Set _Content-Type_ response header passed through `mime.lookup()`.235 *236 * Examples:237 *238 * var filename = 'path/to/image.png';239 * res.contentType(filename);240 * // res.headers['Content-Type'] is now "image/png"241 *242 * res.contentType('.html');243 * res.contentType('html');244 * res.contentType('json');245 * res.contentType('png');Added res.type() alias of res.contentType()Tj Holowaychukbefore adoption54c1a20
line 246why, one step back246 * res.type('png');Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 247–248why, one step back247 *248 * @param {String} typedocsTj Holowaychukbefore adoptionbb9d505
line 249why, one step back249 * @return {ServerResponse} for chainingAdded error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 250–252why, one step back250 * @api public251 */252Added res.type() alias of res.contentType()Tj Holowaychukbefore adoption54c1a20
lines 253–254why, one step back253res.contentType =254res.type = function(type){Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 255–274why, one step back255 return this.header('Content-Type', mime.lookup(type));256};257258/**259 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.260 *261 * @param {String} filename262 * @return {ServerResponse}263 * @api public264 */265266res.attachment = function(filename){267 if (filename) this.contentType(filename);268 this.header('Content-Disposition', filename269 ? 'attachment; filename="' + basename(filename) + '"'270 : 'attachment');271 return this;272};273274/**Added support for `res.set(obj)`. Closes #834Tj Holowaychukbefore adoption021655a
lines 275–276why, one step back275 * Set header `field` to `val`, or pass276 * an object of of header fields.Added `res.set(field, val)` as an alternative to `res.header()`Tj Holowaychukbefore adoption19a1a22
line 277why, one step back277 *Added support for `res.set(obj)`. Closes #834Tj Holowaychukbefore adoption021655a
lines 278–283why, one step back278 * Examples:279 *280 * res.set('Accept', 'application/json');281 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });282 *283 * @param {String|Object} fieldAdded `res.set(field, val)` as an alternative to `res.header()`Tj Holowaychukbefore adoption19a1a22
lines 284–288why, one step back284 * @param {String} val285 * @return {ServerResponse} for chaining286 * @api public287 */288Added `res.get(field)` as an alternative to `res.header(field)`Tj Holowaychukbefore adoptionc8c6aa2
line 289why, one step back289res.set = function(field, val){Added support for `res.set(obj)`. Closes #834Tj Holowaychukbefore adoption021655a
lines 290–296why, one step back290 if (2 == arguments.length) {291 this.setHeader(field, val);292 } else {293 for (var key in field) {294 this.setHeader(key, field[key]);295 }296 }Added `res.set(field, val)` as an alternative to `res.header()`Tj Holowaychukbefore adoption19a1a22
lines 297–300why, one step back297 return this;298};299300/**Added `res.get(field)` as an alternative to `res.header(field)`Tj Holowaychukbefore adoptionc8c6aa2
line 301why, one step back301 * Get value for header `field`.Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
line 302why, one step back302 *Added `res.get(field)` as an alternative to `res.header(field)`Tj Holowaychukbefore adoptionc8c6aa2
lines 303–314why, one step back303 * @param {String} field304 * @return {String}305 * @api public306 */307308res.get = function(field){309 return this.getHeader(field);310};311312/**313 * Set or get response header `field` with optional `val`.314 *Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 315–316why, one step back315 * @param {String} name316 * @param {String} valMerge branch 'remove-header-fields'Tj Holowaychukbefore adoption772c135
line 317why, one step back317 * @return {ServerResponse} for chainingAdded error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 318–320why, one step back318 * @api public319 */320Added `res.get(field)` as an alternative to `res.header(field)`Tj Holowaychukbefore adoptionc8c6aa2
lines 321–323why, one step back321res.header = function(field, val){322 if (1 == arguments.length) return this.getHeader(field);323 this.setHeader(field, val);Refactored `res.header()`Tj Holowaychukbefore adoption54d3751
line 324why, one step back324 return this;Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 325–331why, one step back325};326327/**328 * Clear cookie `name`.329 *330 * @param {String} name331 * @param {Object} optionsChanged `res.{cookie,clearCookie}()` return resTj Holowaychukbefore adoptiona3cddb8
line 332why, one step back332 * @param {ServerResponse} for chainingAdded error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 333–337why, one step back333 * @api public334 */335336res.clearCookie = function(name, options){337 var opts = { expires: new Date(1) };Changed `res.{cookie,clearCookie}()` return resTj Holowaychukbefore adoptiona3cddb8
line 338why, one step back338 return this.cookie(name, '', optionsAdded error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 339–348why, one step back339 ? utils.merge(options, opts)340 : opts);341};342343/**344 * Set cookie `name` to `val`, with the given `options`.345 *346 * Options:347 *348 * - `maxAge` max-age in milliseconds, converted to `expires`Merged basepath setting. Closes #813Tj Holowaychukbefore adoptione9abe1b
line 349why, one step back349 * - `path` defaults to the "basepath" setting which is typically "/"Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 350–367why, one step back350 *351 * Examples:352 *353 * // "Remember Me" for 15 minutes354 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });355 *356 * // save as above357 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })358 *359 * @param {String} name360 * @param {String} val361 * @param {Options} options362 * @api public363 */364365res.cookie = function(name, val, options){366 options = options || {};367 if ('maxAge' in options) options.expires = new Date(Date.now() + options.maxAge);Merged basepath setting. Closes #813Tj Holowaychukbefore adoptione9abe1b
line 368why, one step back368 if (undefined === options.path) options.path = this.app.set('basepath');Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 369–370why, one step back369 var cookie = utils.serializeCookie(name, val, options);370 this.header('Set-Cookie', cookie);Changed `res.{cookie,clearCookie}()` return resTj Holowaychukbefore adoptiona3cddb8
line 371why, one step back371 return this;Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 372–375why, one step back372};373374/**375 * Redirect to the given `url` with optional response `status`Changed `res.redirect()` signature to accept status first. Closes #832Tj Holowaychukbefore adoptione301d43
line 376why, one step back376 * defaulting to 302.Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 377–380why, one step back377 *378 * The given `url` can also be the name of a mapped url, for379 * example by default express supports "back" which redirects380 * to the _Referrer_ or _Referer_ headers or the application'sMerged basepath setting. Closes #813Tj Holowaychukbefore adoptione9abe1b
lines 381–382why, one step back381 * "basepath" setting. Express also supports "basepath" out of the box,382 * which can be set via `app.set('basepath', '/blog');`.Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
line 383why, one step back383 *Changed `res.redirect()` signature to accept status first. Closes #832Tj Holowaychukbefore adoptione301d43
lines 384–389why, one step back384 * Examples:385 *386 * res.redirect('/foo/bar');387 * res.redirect('http://example.com');388 * res.redirect(301, 'http://example.com');389 *Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 390–423why, one step back390 * Redirect Mapping:391 * 392 * To extend the redirect mapping capabilities that Express provides,393 * we may use the `app.redirect()` method:394 * 395 * app.redirect('google', 'http://google.com');396 * 397 * Now in a route we may call:398 *399 * res.redirect('google');400 *401 * We may also map dynamic redirects:402 *403 * app.redirect('comments', function(req, res){404 * return '/post/' + req.params.id + '/comments';405 * });406 *407 * So now we may do the following, and the redirect will dynamically adjust to408 * the context of the request. If we called this route with _GET /post/12_ our409 * redirect _Location_ would be _/post/12/comments_.410 *411 * app.get('/post/:id', function(req, res){412 * res.redirect('comments');413 * });414 *415 * Unless an absolute `url` is given, the app's mount-point416 * will be respected. For example if we redirect to `/posts`,417 * and our app is mounted at `/blog` we will redirect to `/blog/posts`.418 *419 * @param {String} url420 * @param {Number} code421 * @api public422 */423Changed `res.redirect()` signature to accept status first. Closes #832Tj Holowaychukbefore adoptione301d43
line 424why, one step back424res.redirect = function(url){Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 425–426why, one step back425 var app = this.app426 , req = this.reqMerged basepath setting. Closes #813Tj Holowaychukbefore adoptione9abe1b
line 427why, one step back427 , base = app.set('basepath') || app.routeFixed `res.redirect()` HEAD supportTj Holowaychukbefore adoptiond3937b9
line 428why, one step back428 , head = 'HEAD' == req.methodChanged `res.redirect()` signature to accept status first. Closes #832Tj Holowaychukbefore adoptione301d43
line 429why, one step back429 , status = 302Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
line 430why, one step back430 , body;Changed `res.redirect()` signature to accept status first. Closes #832Tj Holowaychukbefore adoptione301d43
lines 431–436why, one step back431432 // allow status / url433 if (2 == arguments.length) {434 status = url;435 url = arguments[1];436 }Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 437–457why, one step back437438 // Setup redirect map439 var map = {440 back: req.header('Referrer', base)441 , home: base442 };443444 // Support custom redirect map445 map.__proto__ = app.redirects;446447 // Attempt mapped redirect448 var mapped = 'function' == typeof map[url]449 ? map[url](req, this)450 : map[url];451452 // Perform redirect453 url = mapped || url;454455 // Relative456 if (!~url.indexOf('://')) {457 // Respect mount-pointMerged basepath setting. Closes #813Tj Holowaychukbefore adoptione9abe1b
line 458why, one step back458 if ('/' != base && 0 != url.indexOf(base)) url = base + url;Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 459–467why, one step back459460 // Absolute461 var host = req.headers.host462 , tls = req.connection.encrypted;463 url = 'http' + (tls ? 's' : '') + '://' + host + url;464 }465466 // Support text/{plain,html} by default467 if (req.accepts('html')) {refactored `res.redirect()` slightlyTj Holowaychukbefore adoption6bd5182
line 468why, one step back468 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 469–470why, one step back469 this.header('Content-Type', 'text/html');470 } else {refactored `res.redirect()` slightlyTj Holowaychukbefore adoption6bd5182
line 471why, one step back471 body = statusCodes[status] + '. Redirecting to ' + url;Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
lines 472–477why, one step back472 this.header('Content-Type', 'text/plain');473 }474475 // Respond476 this.statusCode = status;477 this.header('Location', url);Fixed `res.redirect()` HEAD supportTj Holowaychukbefore adoptiond3937b9
line 478why, one step back478 this.end(head ? null : body);Added error handling to web-service exampleTj Holowaychukbefore adoptione4342a7
line 479why, one step back479};request-level render()Tj Holowaychukbefore adoptionc40c54f
lines 480–503why, one step back480481/**482 * Render `view` with the given `options` and optional callback `fn`.483 * When a callback function is given a response will _not_ be made484 * automatically, otherwise a response of _200_ and _text/html_ is given.485 *486 * Options:487 * 488 * - `status` Response status code (`res.statusCode`)489 * - `charset` Set the charset (`res.charset`)490 *491 * Reserved locals:492 *493 * - `cache` boolean hinting to the engine it should cache494 * - `filename` filename of the view being rendered495 *496 * @param {String} view497 * @param {Object|Function} options or callback function498 * @param {Function} fn499 * @api public500 */501502res.render = function(view, options, fn){503 var self = thismerging res.localsTj Holowaychukbefore adoption98e2c05
line 504why, one step back504 , options = options || {}request-level render()Tj Holowaychukbefore adoptionc40c54f
lines 505–509why, one step back505 , req = this.req506 , app = req.app;507508 // support callback function as second arg509 if ('function' == typeof options) {typoTj Holowaychukbefore adoptionae11d83
line 510why, one step back510 fn = options, options = {};request-level render()Tj Holowaychukbefore adoptionc40c54f
line 511why, one step back511 }merging res.localsTj Holowaychukbefore adoption98e2c05
line 512why, one step back512Added replacement for dynamic locals. Closes #788Tj Holowaychukbefore adoption73f1da2
lines 513–515why, one step back513 function render() {514 // merge res.locals515 options.locals = self.locals;request-level render()Tj Holowaychukbefore adoptionc40c54f
line 516why, one step back516Added replacement for dynamic locals. Closes #788Tj Holowaychukbefore adoption73f1da2
lines 517–518why, one step back517 // status option518 if (options.status) self.statusCode = options.status;added status support backTj Holowaychukbefore adoption31ff7db
line 519why, one step back519Added replacement for dynamic locals. Closes #788Tj Holowaychukbefore adoption73f1da2
lines 520–521why, one step back520 // charset option521 if (options.charset) self.charset = options.charset;added charset option backTj Holowaychukbefore adoptionaab9b08
line 522why, one step back522Added replacement for dynamic locals. Closes #788Tj Holowaychukbefore adoption73f1da2
lines 523–527why, one step back523 // default callback to respond524 fn = fn || function(err, str){525 if (err) return req.next(err);526 self.send(str);527 };request-level render()Tj Holowaychukbefore adoptionc40c54f
line 528why, one step back528Added replacement for dynamic locals. Closes #788Tj Holowaychukbefore adoption73f1da2
lines 529–549why, one step back529 // render530 app.render(view, options, fn);531 }532533 // invoke view callbacks534 var callbacks = app.viewCallbacks535 , pending = callbacks.length536 , len = pending537 , done;538539 for (var i = 0; i < len; ++i) {540 callbacks[i](req, self, function(err){541 if (err) {542 req.next(err);543 done = true;544 return;545 }546547 --pending || render();548 });549 }request-level render()Tj Holowaychukbefore adoptionc40c54f
line 550why, one step back550};