Skip to content
Myr

application.js

This file

Path

lib/application.js

641 lines · 13.8 KiB · JavaScript

Last changed by

chore: replace `methods` dependency with standard library (#6196)Jon Koopsbefore adoptionb11122b

History of this file

Take it

The walk stopped after 200 commits: lines older than 2b2733c 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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

lines 14–19why, one step back
14 */
1516var finalhandler = require('finalhandler');17var debug = require('debug')('express:application');18var View = require('./view');19var http = require('http');

chore: replace `methods` dependency with standard library (#6196)Jon Koopsbefore adoptionb11122b

line 20why, one step back
20var methods = require('./utils').methods;

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

lines 21–23why, one step back
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');

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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;

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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/**

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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 });

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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')

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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)

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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) {

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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, {

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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);

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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);

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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])

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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 }

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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);

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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')

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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;

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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;

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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)

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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);

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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);

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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:

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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);

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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.

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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)

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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) {

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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');

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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) {

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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];

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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() {

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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));

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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) {

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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) {

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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) {

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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 }

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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);

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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);

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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) {

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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;

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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 = {};

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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 = {};

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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);

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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 }

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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);

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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 }

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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 }

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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, {

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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);

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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);

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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 }

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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);

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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}

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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 */

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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 */

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

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 }

deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c

line 641why, one step back
641}