Skip to content
Myr

response.js

This file

Path

lib/response.js

556 lines · 12.8 KiB · JavaScript

Last changed by

Fixed res.clearCookie(), default path to "root" settingTj Holowaychukbefore adoption0f2f3d4

History of this file

Take it

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

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 1–16why, one step back
1
2/*!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.utils

refactored `res.redirect()` slightlyTj Holowaychukbefore adoption6bd5182

line 17why, one step back
17 , statusCodes = http.STATUS_CODES

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 18–24why, one step back
18 , res = http.ServerResponse.prototype
19 , 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 back
25 * Send a response.

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 26–31why, one step back
26 *
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 back
32 * res.send(404, 'Sorry, cant find that');

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 33–34why, one step back
33 * res.send(404);
34 *

Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f

lines 35–36why, one step back
35 * @param {Mixed} body or status
36 * @param {Mixed} body

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 37–40why, one step back
37 * @return {ServerResponse}
38 * @api public39 */40

Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f

line 41why, one step back
41res.send = function(body){

refactored `res.send()`Tj Holowaychukbefore adoption2c734e2

lines 42–44why, one step back
42 var req = this.req
43 , head = 'HEAD' == req.method;44

Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f

lines 45–49why, one step back
45 // allow status / body
46 if (2 == arguments.length) {47 this.statusCode = body;48 body = arguments[1];49 }

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 50–51why, one step back
50
51 switch (typeof body) {

Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f

line 52why, one step back
52 // response status

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

line 53why, one step back
53 case 'number':

Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f

lines 54–56why, one step back
54 this.header('Content-Type') || this.contentType('.txt');
55 this.statusCode = body;56 body = http.STATUS_CODES[body];

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

line 57why, one step back
57 break;

Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f

line 58why, one step back
58 // string defaulting to html

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 59–66why, one step back
59 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 back
67 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 back
70 this.header('Content-Type') || this.contentType('.bin');

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

line 71why, one step back
71 } else {

Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f

line 72why, one step back
72 return this.json(body);

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 73–77why, one step back
73 }
74 break;75 }7677 // populate Content-Length

Fixed [23]04 supportTj Holowaychukbefore adoption5ac5352

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

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 79–84why, one step back
79 this.header('Content-Length', Buffer.isBuffer(body)
80 ? body.length81 : Buffer.byteLength(body));82 }8384 // strip irrelevant headers

Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f

line 85why, one step back
85 if (204 == this.statusCode || 304 == this.statusCode) {

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 86–87why, one step back
86 this.removeHeader('Content-Type');
87 this.removeHeader('Content-Length');

Fixed 204 / 304 responsesTj Holowaychukbefore adoptionc631eb4

line 88why, one step back
88 body = '';

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 89–91why, one step back
89 }
9091 // respond

refactored `res.send()`Tj Holowaychukbefore adoption2c734e2

line 92why, one step back
92 this.end(head ? null : body);

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 93–96why, one step back
93 return this;
94};9596/**

Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f

line 97why, one step back
97 * Send JSON response.

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 98–102why, one step back
98 *
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 back
103 * res.json(500, 'oh noes!');
104 * res.json(404, 'I dont have that');

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

line 105why, one step back
105 *

Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f

line 106why, one step back
106 * @param {Mixed} obj or status

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 107–111why, one step back
107 * @param {Mixed} obj
108 * @return {ServerResponse}109 * @api public110 */111

Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f

lines 112–118why, one step back
112res.json = function(obj){
113 // allow status / body114 if (2 == arguments.length) {115 this.statusCode = obj;116 obj = arguments[1];117 }118

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 119–130why, one step back
119 var body = JSON.stringify(obj)
120 , callback = this.req.query.callback121 , jsonp = this.app.enabled('jsonp callback');122123 this.charset = this.charset || 'utf-8';124 this.header('Content-Type', 'application/json');125126 if (callback && jsonp) {127 this.header('Content-Type', 'text/javascript');128 body = callback.replace(/[^\w$.]/g, '') + '(' + body + ');';129 }130

Changed `res.{send,json}()` status code to the first arg at all timesTj Holowaychukbefore adoptionc35cd7f

line 131why, one step back
131 return this.send(body);

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 132–147why, one step back
132};
133134/**135 * Set status `code`.136 *137 * @param {Number} code138 * @return {ServerResponse}139 * @api public140 */141142res.status = function(code){143 this.statusCode = code;144 return this;145};146147/**

refactored `res.sendfile()`Tj Holowaychukbefore adoption5da28be

lines 148–151why, one step back
148 * 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 complete

make `res.sendfile()` more like `res.download()`. Closes #774Tj Holowaychukbefore adoption7a476fc

line 152why, one step back
152 * or when an error occurs. Be sure to check `res.sentHeader`

refactored `res.sendfile()`Tj Holowaychukbefore adoption5da28be

lines 153–154why, one step back
153 * if you wish to attempt responding, as the header and some data
154 * may have already been transferred.

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 155–167why, one step back
155 *
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 back
168 var self = this

fixed `res.sendfile()` 404 supportTj Holowaychukbefore adoptiond2df055

line 169why, one step back
169 , req = self.req

refactored `res.sendfile()`Tj Holowaychukbefore adoption5da28be

lines 170–171why, one step back
170 , next = this.req.next
171 , options = options || {};

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 172–178why, one step back
172
173 // support function as second arg174 if ('function' == typeof options) {175 fn = options;176 options = {};177 }178

refactored `res.sendfile()`Tj Holowaychukbefore adoption5da28be

lines 179–181why, one step back
179 // callback
180 options.callback = function(err){181 // TODO: remove when node adds this

make `res.sendfile()` more like `res.download()`. Closes #774Tj Holowaychukbefore adoption7a476fc

lines 182–188why, one step back
182 self.sentHeader = !! self._header;
183184 // error185 if (err) {186 // ditch content-disposition to prevent funky responses187 if (!self.sentHeader) self.removeHeader('Content-Disposition');188

fixed `res.sendfile()` 404 supportTj Holowaychukbefore adoptiond2df055

lines 189–191why, one step back
189 // not found
190 if ('ENOENT' == err.code) return req.next();191

make `res.sendfile()` more like `res.download()`. Closes #774Tj Holowaychukbefore adoption7a476fc

lines 192–197why, one step back
192 // lost in limbo if there's no callback
193 if (self.sentHeader) return fn && fn(err);194195 // callback available196 if (fn) return fn(err);197

fixed `res.sendfile()` 404 supportTj Holowaychukbefore adoptiond2df055

line 198why, one step back
198 return req.next(err);

make `res.sendfile()` more like `res.download()`. Closes #774Tj Holowaychukbefore adoption7a476fc

lines 199–201why, one step back
199 }
200201 fn && fn();

refactored `res.sendfile()`Tj Holowaychukbefore adoption5da28be

lines 202–204why, one step back
202 };
203204 // transfer

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 205–209why, one step back
205 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 back
210 * 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/**

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 234–245why, one step back
234 * 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 back
246 * res.type('png');

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 247–248why, one step back
247 *
248 * @param {String} type

docsTj Holowaychukbefore adoptionbb9d505

line 249why, one step back
249 * @return {ServerResponse} for chaining

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 250–252why, one step back
250 * @api public
251 */252

Added res.type() alias of res.contentType()Tj Holowaychukbefore adoption54c1a20

lines 253–254why, one step back
253res.contentType =
254res.type = function(type){

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 255–274why, one step back
255 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 back
275 * Set header `field` to `val`, or pass
276 * an object of of header fields.

Added `res.set(field, val)` as an alternative to `res.header()`Tj Holowaychukbefore adoption19a1a22

line 277why, one step back
277 *

Added support for `res.set(obj)`. Closes #834Tj Holowaychukbefore adoption021655a

lines 278–283why, one step back
278 * Examples:
279 *280 * res.set('Accept', 'application/json');281 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });282 *283 * @param {String|Object} field

Added `res.set(field, val)` as an alternative to `res.header()`Tj Holowaychukbefore adoption19a1a22

lines 284–288why, one step back
284 * @param {String} val
285 * @return {ServerResponse} for chaining286 * @api public287 */288

Added `res.get(field)` as an alternative to `res.header(field)`Tj Holowaychukbefore adoptionc8c6aa2

line 289why, one step back
289res.set = function(field, val){

Added support for `res.set(obj)`. Closes #834Tj Holowaychukbefore adoption021655a

lines 290–296why, one step back
290 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 back
297 return this;
298};299300/**

Added `res.get(field)` as an alternative to `res.header(field)`Tj Holowaychukbefore adoptionc8c6aa2

line 301why, one step back
301 * Get value for header `field`.

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

line 302why, one step back
302 *

Added `res.get(field)` as an alternative to `res.header(field)`Tj Holowaychukbefore adoptionc8c6aa2

lines 303–314why, one step back
303 * @param {String} field
304 * @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 *

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 315–316why, one step back
315 * @param {String} name
316 * @param {String} val

Merge branch 'remove-header-fields'Tj Holowaychukbefore adoption772c135

line 317why, one step back
317 * @return {ServerResponse} for chaining

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 318–320why, one step back
318 * @api public
319 */320

Added `res.get(field)` as an alternative to `res.header(field)`Tj Holowaychukbefore adoptionc8c6aa2

lines 321–323why, one step back
321res.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 back
324 return this;

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 325–331why, one step back
325};
326327/**328 * Clear cookie `name`.329 *330 * @param {String} name331 * @param {Object} options

Changed `res.{cookie,clearCookie}()` return resTj Holowaychukbefore adoptiona3cddb8

line 332why, one step back
332 * @param {ServerResponse} for chaining

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 333–336why, one step back
333 * @api public
334 */335336res.clearCookie = function(name, options){

Fixed res.clearCookie(), default path to "root" settingTj Holowaychukbefore adoption0f2f3d4

line 337why, one step back
337 var opts = { expires: new Date(1), path: this.app.get('root') };

Changed `res.{cookie,clearCookie}()` return resTj Holowaychukbefore adoptiona3cddb8

line 338why, one step back
338 return this.cookie(name, '', options

Fixed res.clearCookie(), default path to "root" settingTj Holowaychukbefore adoption0f2f3d4

line 339why, one step back
339 ? utils.merge(opts, options)

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 340–348why, one step back
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 back
349 * - `path` defaults to the "basepath" setting which is typically "/"

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 350–367why, one step back
350 *
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 back
368 if (undefined === options.path) options.path = this.app.set('basepath');

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 369–370why, one step back
369 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 back
371 return this;

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 372–375why, one step back
372};
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 back
376 * defaulting to 302.

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 377–380why, one step back
377 *
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's

Merged basepath setting. Closes #813Tj Holowaychukbefore adoptione9abe1b

lines 381–382why, one step back
381 * "basepath" setting. Express also supports "basepath" out of the box,
382 * which can be set via `app.set('basepath', '/blog');`.

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

line 383why, one step back
383 *

Changed `res.redirect()` signature to accept status first. Closes #832Tj Holowaychukbefore adoptione301d43

lines 384–389why, one step back
384 * Examples:
385 *386 * res.redirect('/foo/bar');387 * res.redirect('http://example.com');388 * res.redirect(301, 'http://example.com');389 *

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 390–423why, one step back
390 * 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 */423

Changed `res.redirect()` signature to accept status first. Closes #832Tj Holowaychukbefore adoptione301d43

line 424why, one step back
424res.redirect = function(url){

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 425–426why, one step back
425 var app = this.app
426 , req = this.req

Merged basepath setting. Closes #813Tj Holowaychukbefore adoptione9abe1b

line 427why, one step back
427 , base = app.set('basepath') || app.route

Fixed `res.redirect()` HEAD supportTj Holowaychukbefore adoptiond3937b9

line 428why, one step back
428 , head = 'HEAD' == req.method

Changed `res.redirect()` signature to accept status first. Closes #832Tj Holowaychukbefore adoptione301d43

line 429why, one step back
429 , status = 302

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

line 430why, one step back
430 , body;

Changed `res.redirect()` signature to accept status first. Closes #832Tj Holowaychukbefore adoptione301d43

lines 431–436why, one step back
431
432 // allow status / url433 if (2 == arguments.length) {434 status = url;435 url = arguments[1];436 }

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 437–457why, one step back
437
438 // 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-point

Merged basepath setting. Closes #813Tj Holowaychukbefore adoptione9abe1b

line 458why, one step back
458 if ('/' != base && 0 != url.indexOf(base)) url = base + url;

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 459–461why, one step back
459
460 // Absolute461 var host = req.headers.host

Added `req.secure`Tj Holowaychukbefore adoptionde17b28

line 462why, one step back
462 , tls = req.secure;

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 463–467why, one step back
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 back
468 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 469–470why, one step back
469 this.header('Content-Type', 'text/html');
470 } else {

refactored `res.redirect()` slightlyTj Holowaychukbefore adoption6bd5182

line 471why, one step back
471 body = statusCodes[status] + '. Redirecting to ' + url;

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

lines 472–477why, one step back
472 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 back
478 this.end(head ? null : body);

utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd

line 479why, one step back
479};

request-level render()Tj Holowaychukbefore adoptionc40c54f

lines 480–503why, one step back
480
481/**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 = this

merging res.localsTj Holowaychukbefore adoption98e2c05

line 504why, one step back
504 , options = options || {}

request-level render()Tj Holowaychukbefore adoptionc40c54f

lines 505–509why, one step back
505 , req = this.req
506 , app = req.app;507508 // support callback function as second arg509 if ('function' == typeof options) {

typoTj Holowaychukbefore adoptionae11d83

line 510why, one step back
510 fn = options, options = {};

request-level render()Tj Holowaychukbefore adoptionc40c54f

line 511why, one step back
511 }

merging res.localsTj Holowaychukbefore adoption98e2c05

line 512why, one step back
512

Added replacement for dynamic locals. Closes #788Tj Holowaychukbefore adoption73f1da2

lines 513–515why, one step back
513 function render() {
514 // merge res.locals515 options.locals = self.locals;

request-level render()Tj Holowaychukbefore adoptionc40c54f

line 516why, one step back
516

Added replacement for dynamic locals. Closes #788Tj Holowaychukbefore adoption73f1da2

lines 517–518why, one step back
517 // status option
518 if (options.status) self.statusCode = options.status;

added status support backTj Holowaychukbefore adoption31ff7db

line 519why, one step back
519

Added replacement for dynamic locals. Closes #788Tj Holowaychukbefore adoption73f1da2

lines 520–521why, one step back
520 // charset option
521 if (options.charset) self.charset = options.charset;

added charset option backTj Holowaychukbefore adoptionaab9b08

line 522why, one step back
522

Added replacement for dynamic locals. Closes #788Tj Holowaychukbefore adoption73f1da2

lines 523–527why, one step back
523 // default callback to respond
524 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 back
528

Added replacement for dynamic locals. Closes #788Tj Holowaychukbefore adoption73f1da2

lines 529–538why, one step back
529 // render
530 app.render(view, options, fn);531 }532533 // invoke view callbacks534 var callbacks = app.viewCallbacks535 , pending = callbacks.length536 , len = pending537 , done;538

refactored res.render()Tj Holowaychukbefore adoptionb433550

lines 539–542why, one step back
539 if (len) {
540 for (var i = 0; i < len; ++i) {541 callbacks[i](req, self, function(err){542 if (done) return;

Added replacement for dynamic locals. Closes #788Tj Holowaychukbefore adoption73f1da2

line 543why, one step back
543

refactored res.render()Tj Holowaychukbefore adoptionb433550

lines 544–554why, one step back
544 if (err) {
545 req.next(err);546 done = true;547 return;548 }549550 --pending || render();551 });552 }553 } else {554 render();

Added replacement for dynamic locals. Closes #788Tj Holowaychukbefore adoption73f1da2

line 555why, one step back
555 }

request-level render()Tj Holowaychukbefore adoptionc40c54f

line 556why, one step back
556};