Skip to content
Myr

application.js

This file

Path

lib/application.js

641 lines · 13.8 KiB · JavaScript

Last changed by

refactor: Remove Object.setPrototypeOf polyfill (#6081)Phillip Bartabefore adoption8cb53ea

History of this file

Take it

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 1–8why, one step back
1/*!
2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2013 Roman Shtylman5 * Copyright(c) 2014-2015 Douglas Christopher Wilson6 * MIT Licensed7 */8

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 9–10why, one step back
9'use strict';
10

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 11–12why, one step back
11/**
12 * Module dependencies.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 13why, one step back
13 * @private

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 14–23why, one step back
14 */
1516var finalhandler = require('finalhandler');17var methods = require('methods');18var debug = require('debug')('express:application');19var View = require('./view');20var http = require('http');21var compileETag = require('./utils').compileETag;22var compileQueryParser = require('./utils').compileQueryParser;23var compileTrust = require('./utils').compileTrust;

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 24why, one step back
24var merge = require('utils-merge');

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 25why, one step back
25var resolve = require('path').resolve;

call callback once on listen errorWesbefore adoptionee40a88

line 26why, one step back
26var once = require('once')

Use router module for routingDouglas Christopher Wilsonbefore adoptioncec5780

line 27why, one step back
27var Router = require('router');

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 28–33why, one step back
28
29/**30 * Module variables.31 * @private32 */33

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 34why, one step back
34var slice = Array.prototype.slice;

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

line 35why, one step back
35var flatten = Array.prototype.flat;

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 36–43why, one step back
36
37/**38 * Application prototype.39 */4041var app = exports = module.exports = {};4243/**

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

line 44why, one step back
44 * Variable for trust proxy inheritance back-compat

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 45why, one step back
45 * @private

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 46–50why, one step back
46 */
4748var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';4950/**

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 51–56why, one step back
51 * Initialize the server.
52 *53 * - setup default configuration54 * - setup default middleware55 * - setup route reflection methods56 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 57why, one step back
57 * @private

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 58–59why, one step back
58 */
59

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 60why, one step back
60app.init = function init() {

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

lines 61–62why, one step back
61 var router = null;
62

Use object with null prototype for various app propertiesEvan Hahnbefore adoption1443973

lines 63–65why, one step back
63 this.cache = Object.create(null);
64 this.engines = Object.create(null);65 this.settings = Object.create(null);

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 66why, one step back
66

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 67why, one step back
67 this.defaultConfiguration();

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

lines 68–83why, one step back
68
69 // Setup getting to lazily add base router70 Object.defineProperty(this, 'router', {71 configurable: true,72 enumerable: true,73 get: function getrouter() {74 if (router === null) {75 router = new Router({76 caseSensitive: this.enabled('case sensitive routing'),77 strict: this.enabled('strict routing')78 });79 }8081 return router;82 }83 });

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 84–87why, one step back
84};
8586/**87 * Initialize application configuration.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 88why, one step back
88 * @private

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 89–90why, one step back
89 */
90

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 91–93why, one step back
91app.defaultConfiguration = function defaultConfiguration() {
92 var env = process.env.NODE_ENV || 'development';93

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 94–97why, one step back
94 // default settings
95 this.enable('x-powered-by');96 this.set('etag', 'weak');97 this.set('env', env);

Change query parser setting default to 'simple'Czarekbefore adoption450c468

line 98why, one step back
98 this.set('query parser', 'simple')

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 99–101why, one step back
99 this.set('subdomain offset', 2);
100 this.set('trust proxy', false);101

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 102–107why, one step back
102 // trust proxy inherit back-compat
103 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {104 configurable: true,105 value: true106 });107

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 108–109why, one step back
108 debug('booting in %s mode', env);
109

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 110–118why, one step back
110 this.on('mount', function onmount(parent) {
111 // inherit trust proxy112 if (this.settings[trustProxyDefaultSymbol] === true113 && typeof parent.settings['trust proxy fn'] === 'function') {114 delete this.settings['trust proxy'];115 delete this.settings['trust proxy fn'];116 }117118 // inherit protos

refactor: Remove Object.setPrototypeOf polyfill (#6081)Phillip Bartabefore adoption8cb53ea

lines 119–122why, one step back
119 Object.setPrototypeOf(this.request, parent.request)
120 Object.setPrototypeOf(this.response, parent.response)121 Object.setPrototypeOf(this.engines, parent.engines)122 Object.setPrototypeOf(this.settings, parent.settings)

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 123–146why, one step back
123 });
124125 // setup locals126 this.locals = Object.create(null);127128 // top-most app is mounted at /129 this.mountpath = '/';130131 // default locals132 this.locals.settings = this.settings;133134 // default configuration135 this.set('view', View);136 this.set('views', resolve('views'));137 this.set('jsonp callback name', 'callback');138139 if (env === 'production') {140 this.enable('view cache');141 }142};143144/**145 * Dispatch a req, res pair into the application. Starts pipeline processing.146 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 147why, one step back
147 * If no callback is provided, then default error handlers will respond

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 148–149why, one step back
148 * in the event of an error bubbling through the stack.
149 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 150why, one step back
150 * @private

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 151–152why, one step back
151 */
152

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 153why, one step back
153app.handle = function handle(req, res, callback) {

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 154why, one step back
154 // final handler

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 155why, one step back
155 var done = callback || finalhandler(req, res, {

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 156–159why, one step back
156 env: this.get('env'),
157 onerror: logerror.bind(this)158 });159

Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054

lines 160–169why, one step back
160 // set powered by header
161 if (this.enabled('x-powered-by')) {162 res.setHeader('X-Powered-By', 'Express');163 }164165 // set circular references166 req.res = res;167 res.req = req;168169 // alter the prototypes

refactor: Remove Object.setPrototypeOf polyfill (#6081)Phillip Bartabefore adoption8cb53ea

lines 170–171why, one step back
170 Object.setPrototypeOf(req, this.request)
171 Object.setPrototypeOf(res, this.response)

Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054

lines 172–175why, one step back
172
173 // setup locals174 if (!res.locals) {175 res.locals = Object.create(null);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 176–177why, one step back
176 }
177

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

line 178why, one step back
178 this.router.handle(req, res, done);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 179–187why, one step back
179};
180181/**182 * Proxy `Router#use()` to add middleware to the app router.183 * See Router#use() documentation for details.184 *185 * If the _fn_ parameter is an express app, then it will be186 * mounted at the _route_ specified.187 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 188why, one step back
188 * @public

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 189–195why, one step back
189 */
190191app.use = function use(fn) {192 var offset = 0;193 var path = '/';194195 // default path to '/'

Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f

line 196why, one step back
196 // disambiguate app.use([fn])

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 197why, one step back
197 if (typeof fn !== 'function') {

Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f

lines 198–203why, one step back
198 var arg = fn;
199200 while (Array.isArray(arg) && arg.length !== 0) {201 arg = arg[0];202 }203

Fix regression for empty string path in app.useDouglas Christopher Wilsonbefore adoption3c1a964

lines 204–205why, one step back
204 // first arg is the path
205 if (typeof arg !== 'function') {

Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f

lines 206–208why, one step back
206 offset = 1;
207 path = fn;208 }

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 209–210why, one step back
209 }
210

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

line 211why, one step back
211 var fns = flatten.call(slice.call(arguments, offset), Infinity);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 212–213why, one step back
212
213 if (fns.length === 0) {

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 214why, one step back
214 throw new TypeError('app.use() requires a middleware function')

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 215–216why, one step back
215 }
216

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

lines 217–218why, one step back
217 // get router
218 var router = this.router;

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 219–227why, one step back
219
220 fns.forEach(function (fn) {221 // non-express app222 if (!fn || !fn.handle || !fn.set) {223 return router.use(path, fn);224 }225226 debug('.use app under %s', path);227 fn.mountpath = path;

Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4

line 228why, one step back
228 fn.parent = this;

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 229–233why, one step back
229
230 // restore .app property on req and res231 router.use(path, function mounted_app(req, res, next) {232 var orig = req.app;233 fn.handle(req, res, function (err) {

refactor: Remove Object.setPrototypeOf polyfill (#6081)Phillip Bartabefore adoption8cb53ea

lines 234–235why, one step back
234 Object.setPrototypeOf(req, orig.request)
235 Object.setPrototypeOf(res, orig.response)

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 236–240why, one step back
236 next(err);
237 });238 });239240 // mounted an app

Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4

lines 241–242why, one step back
241 fn.emit('mount', this);
242 }, this);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 243–253why, one step back
243
244 return this;245};246247/**248 * Proxy to the app `Router#route()`249 * Returns a new `Route` instance for the _path_.250 *251 * Routes are isolated middleware stacks for specific paths.252 * See the Route api docs for details.253 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 254why, one step back
254 * @public

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 255–256why, one step back
255 */
256

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

lines 257–258why, one step back
257app.route = function route(path) {
258 return this.router.route(path);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 259–266why, one step back
259};
260261/**262 * Register the given template engine callback `fn`263 * as `ext`.264 *265 * By default will `require()` the engine based on the266 * file extension. For example if you try to render

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 267why, one step back
267 * a "foo.ejs" file Express will invoke the following internally:

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 268why, one step back
268 *

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 269why, one step back
269 * app.engine('ejs', require('ejs').__express);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 270–280why, one step back
270 *
271 * For engines that do not provide `.__express` out of the box,272 * or if you wish to "map" a different extension to the template engine273 * you may use this method. For example mapping the EJS template engine to274 * ".html" files:275 *276 * app.engine('html', require('ejs').renderFile);277 *278 * In this case EJS provides a `.renderFile()` method with279 * the same signature that Express expects: `(path, options, callback)`,280 * though note that it aliases this method as `ejs.__express` internally

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 281why, one step back
281 * so if you're using ".ejs" extensions you don't need to do anything.

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 282–283why, one step back
282 *
283 * Some template engines do not follow this convention, the

docs: visionmedia is now tj on GithubDouglas Christopher Wilsonbefore adoptione9539fc

line 284why, one step back
284 * [Consolidate.js](https://github.com/tj/consolidate.js)

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 285–291why, one step back
285 * library was created to map all of node's popular template
286 * engines to follow this convention, thus allowing them to287 * work seamlessly within Express.288 *289 * @param {String} ext290 * @param {Function} fn291 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 292why, one step back
292 * @public

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 293–294why, one step back
293 */
294

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 295–307why, one step back
295app.engine = function engine(ext, fn) {
296 if (typeof fn !== 'function') {297 throw new Error('callback function required');298 }299300 // get file extension301 var extension = ext[0] !== '.'302 ? '.' + ext303 : ext;304305 // store engine306 this.engines[extension] = fn;307

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 308–319why, one step back
308 return this;
309};310311/**312 * Proxy to `Router#param()` with one added api feature. The _name_ parameter313 * can be an array of names.314 *315 * See the Router#param() docs for more details.316 *317 * @param {String|Array} name318 * @param {Function} fn319 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 320why, one step back
320 * @public

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 321–322why, one step back
321 */
322

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

line 323why, one step back
323app.param = function param(name, fn) {

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 324why, one step back
324 if (Array.isArray(name)) {

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

lines 325–327why, one step back
325 for (var i = 0; i < name.length; i++) {
326 this.param(name[i], fn);327 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 328why, one step back
328

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 329–331why, one step back
329 return this;
330 }331

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

line 332why, one step back
332 this.router.param(name, fn);

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 333why, one step back
333

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 334–340why, one step back
334 return this;
335};336337/**338 * Assign `setting` to `val`, or return `setting`'s value.339 *340 * app.set('foo', 'bar');

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 341why, one step back
341 * app.set('foo');

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 342–348why, one step back
342 * // => "bar"
343 *344 * Mounted servers inherit their parent server's settings.345 *346 * @param {String} setting347 * @param {*} [val]348 * @return {Server} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 349why, one step back
349 * @public

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 350–351why, one step back
350 */
351

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 352why, one step back
352app.set = function set(setting, val) {

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 353–354why, one step back
353 if (arguments.length === 1) {
354 // app.get(setting)

Use object with null prototype for settings closes #4835Evan Hahnbefore adoption26801a0

line 355why, one step back
355 return this.settings[setting];

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 356–357why, one step back
356 }
357

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 358–359why, one step back
358 debug('set "%s" to %o', setting, val);
359

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 360–372why, one step back
360 // set value
361 this.settings[setting] = val;362363 // trigger matched settings364 switch (setting) {365 case 'etag':366 this.set('etag fn', compileETag(val));367 break;368 case 'query parser':369 this.set('query parser fn', compileQueryParser(val));370 break;371 case 'trust proxy':372 this.set('trust proxy fn', compileTrust(val));

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 373–379why, one step back
373
374 // trust proxy inherit back-compat375 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {376 configurable: true,377 value: false378 });379

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 380–396why, one step back
380 break;
381 }382383 return this;384};385386/**387 * Return the app's absolute pathname388 * based on the parent(s) that have389 * mounted it.390 *391 * For example if the application was392 * mounted as "/admin", which itself393 * was mounted as "/blog" then the394 * return value would be "/blog/admin".395 *396 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 397why, one step back
397 * @private

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 398–399why, one step back
398 */
399

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 400why, one step back
400app.path = function path() {

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 401–417why, one step back
401 return this.parent
402 ? this.parent.path() + this.mountpath403 : '';404};405406/**407 * Check if `setting` is enabled (truthy).408 *409 * app.enabled('foo')410 * // => false411 *412 * app.enable('foo')413 * app.enabled('foo')414 * // => true415 *416 * @param {String} setting417 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 418why, one step back
418 * @public

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 419–420why, one step back
419 */
420

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 421–422why, one step back
421app.enabled = function enabled(setting) {
422 return Boolean(this.set(setting));

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 423–436why, one step back
423};
424425/**426 * Check if `setting` is disabled.427 *428 * app.disabled('foo')429 * // => true430 *431 * app.enable('foo')432 * app.disabled('foo')433 * // => false434 *435 * @param {String} setting436 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 437why, one step back
437 * @public

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 438–439why, one step back
438 */
439

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 440why, one step back
440app.disabled = function disabled(setting) {

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 441–448why, one step back
441 return !this.set(setting);
442};443444/**445 * Enable `setting`.446 *447 * @param {String} setting448 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 449why, one step back
449 * @public

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 450–451why, one step back
450 */
451

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 452why, one step back
452app.enable = function enable(setting) {

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 453–460why, one step back
453 return this.set(setting, true);
454};455456/**457 * Disable `setting`.458 *459 * @param {String} setting460 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 461why, one step back
461 * @public

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 462–463why, one step back
462 */
463

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 464why, one step back
464app.disable = function disable(setting) {

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 465–473why, one step back
465 return this.set(setting, false);
466};467468/**469 * Delegate `.VERB(...)` calls to `router.VERB(...)`.470 */471472methods.forEach(function(method){473 app[method] = function(path){

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 474–477why, one step back
474 if (method === 'get' && arguments.length === 1) {
475 // app.get(setting)476 return this.set(path);477 }

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 478why, one step back
478

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

line 479why, one step back
479 var route = this.route(path);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 480–491why, one step back
480 route[method].apply(route, slice.call(arguments, 1));
481 return this;482 };483});484485/**486 * Special-cased "all" method, applying the given route `path`,487 * middleware, and callback to _every_ HTTP method.488 *489 * @param {String} path490 * @param {Function} ...491 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 492why, one step back
492 * @public

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 493–494why, one step back
493 */
494

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 495why, one step back
495app.all = function all(path) {

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

line 496why, one step back
496 var route = this.route(path);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 497–498why, one step back
497 var args = slice.call(arguments, 1);
498

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 499–502why, one step back
499 for (var i = 0; i < methods.length; i++) {
500 route[methods[i]].apply(route, args);501 }502

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 503–517why, one step back
503 return this;
504};505506/**507 * Render the given view `name` name with `options`508 * and a callback accepting an error and the509 * rendered template string.510 *511 * Example:512 *513 * app.render('email', { name: 'Tobi' }, function(err, html){514 * // ...515 * })516 *517 * @param {String} name

Merge tag '4.13.4'Douglas Christopher Wilsonbefore adoption7cafdb5

line 518why, one step back
518 * @param {Object|Function} options or fn

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 519–520why, one step back
519 * @param {Function} callback
520 * @public

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 521–522why, one step back
521 */
522

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 523why, one step back
523app.render = function render(name, options, callback) {

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 524why, one step back
524 var cache = this.cache;

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 525why, one step back
525 var done = callback;

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 526why, one step back
526 var engines = this.engines;

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 527–528why, one step back
527 var opts = options;
528 var renderOptions = {};

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 529–531why, one step back
529 var view;
530531 // support callback function as second arg

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 532–534why, one step back
532 if (typeof options === 'function') {
533 done = options;534 opts = {};

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 535–537why, one step back
535 }
536537 // merge app.locals

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 538why, one step back
538 merge(renderOptions, this.locals);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 539–540why, one step back
539
540 // merge options._locals

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 541–542why, one step back
541 if (opts._locals) {
542 merge(renderOptions, opts._locals);

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 543why, one step back
543 }

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 544–545why, one step back
544
545 // merge options

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 546why, one step back
546 merge(renderOptions, opts);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 547–548why, one step back
547
548 // set .cache unless explicitly provided

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 549–551why, one step back
549 if (renderOptions.cache == null) {
550 renderOptions.cache = this.enabled('view cache');551 }

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 552–553why, one step back
552
553 // primed cache

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 554–556why, one step back
554 if (renderOptions.cache) {
555 view = cache[name];556 }

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 557–559why, one step back
557
558 // view559 if (!view) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 560–562why, one step back
560 var View = this.get('view');
561562 view = new View(name, {

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 563–568why, one step back
563 defaultEngine: this.get('view engine'),
564 root: this.get('views'),565 engines: engines566 });567568 if (!view.path) {

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 569–572why, one step back
569 var dirs = Array.isArray(view.root) && view.root.length > 1
570 ? 'directories "' + view.root.slice(0, -1).join('", "') + '" or "' + view.root[view.root.length - 1] + '"'571 : 'directory "' + view.root + '"'572 var err = new Error('Failed to lookup view "' + name + '" in views ' + dirs);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 573why, one step back
573 err.view = view;

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 574why, one step back
574 return done(err);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 575–577why, one step back
575 }
576577 // prime the cache

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 578–580why, one step back
578 if (renderOptions.cache) {
579 cache[name] = view;580 }

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 581–583why, one step back
581 }
582583 // render

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 584why, one step back
584 tryRender(view, renderOptions, done);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 585–604why, one step back
585};
586587/**588 * Listen for connections.589 *590 * A node `http.Server` is returned, with this591 * application (which is a `Function`) as its592 * callback. If you wish to create both an HTTP593 * and HTTPS server you may do so with the "http"594 * and "https" modules as shown here:595 *596 * var http = require('http')597 * , https = require('https')598 * , express = require('express')599 * , app = express();600 *601 * http.createServer(app).listen(80);602 * https.createServer({ ... }, app).listen(443);603 *604 * @return {http.Server}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 605why, one step back
605 * @public

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 606–607why, one step back
606 */
607

call callback once on listen errorWesbefore adoptionee40a88

lines 608–616why, one step back
608app.listen = function listen () {
609 var server = http.createServer(this)610 var args = Array.prototype.slice.call(arguments)611 if (typeof args[args.length - 1] === 'function') {612 var done = args[args.length - 1] = once(args[args.length - 1])613 server.once('error', done)614 }615 return server.listen.apply(server, args)616}

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 617–618why, one step back
617
618/**

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 619–623why, one step back
619 * Log error using console.error.
620 *621 * @param {Error} err622 * @private623 */

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 624why, one step back
624

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 625–626why, one step back
625function logerror(err) {
626 /* istanbul ignore next */

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 627why, one step back
627 if (this.get('env') !== 'test') console.error(err.stack || err.toString());

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 628–640why, one step back
628}
629630/**631 * Try rendering a view.632 * @private633 */634635function tryRender(view, options, callback) {636 try {637 view.render(options, callback);638 } catch (err) {639 callback(err);640 }

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 641why, one step back
641}