Skip to content
Myr

application.js

This file

Path

lib/application.js

647 lines · 13.8 KiB · JavaScript

Last changed by

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

History of this file

Take it

The walk stopped after 200 commits: lines older than fa6a405 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: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 14–21why, 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;

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

deps: array-flatten@3.0.0Douglas Christopher Wilsonbefore adoption3d05e85

line 24why, one step back
24var flatten = require('array-flatten').flatten

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Use router module for routingDouglas Christopher Wilsonbefore adoptioncec5780

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

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

line 28why, one step back
28var setPrototypeOf = require('setprototypeof')

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 29–35why, one step back
29
30/**31 * Module variables.32 * @private33 */3435var hasOwnProperty = Object.prototype.hasOwnProperty

Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 37–44why, one step back
37
38/**39 * Application prototype.40 */4142var app = exports = module.exports = {};4344/**

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 46why, one step back
46 * @private

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 47–51why, one step back
47 */
4849var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';5051/**

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 52–57why, one step back
52 * Initialize the server.
53 *54 * - setup default configuration55 * - setup default middleware56 * - setup route reflection methods57 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 58why, one step back
58 * @private

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 64–65why, one step back
64 this.cache = {};
65 this.engines = {};

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 66–67why, one step back
66 this.settings = {};
67

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 85–88why, one step back
85};
8687/**88 * Initialize application configuration.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 89why, one step back
89 * @private

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Change query parser setting default to 'simple'Czarekbefore adoption450c468

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 111–119why, one step back
111 this.on('mount', function onmount(parent) {
112 // inherit trust proxy113 if (this.settings[trustProxyDefaultSymbol] === true114 && typeof parent.settings['trust proxy fn'] === 'function') {115 delete this.settings['trust proxy'];116 delete this.settings['trust proxy fn'];117 }118119 // inherit protos

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 124–147why, one step back
124 });
125126 // setup locals127 this.locals = Object.create(null);128129 // top-most app is mounted at /130 this.mountpath = '/';131132 // default locals133 this.locals.settings = this.settings;134135 // default configuration136 this.set('view', View);137 this.set('views', resolve('views'));138 this.set('jsonp callback name', 'callback');139140 if (env === 'production') {141 this.enable('view cache');142 }143};144145/**146 * Dispatch a req, res pair into the application. Starts pipeline processing.147 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 151why, one step back
151 * @private

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054

lines 161–170why, one step back
161 // set powered by header
162 if (this.enabled('x-powered-by')) {163 res.setHeader('X-Powered-By', 'Express');164 }165166 // set circular references167 req.res = res;168 res.req = req;169170 // alter the prototypes

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

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

Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054

lines 173–176why, one step back
173
174 // setup locals175 if (!res.locals) {176 res.locals = Object.create(null);

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 189why, one step back
189 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 190–191why, one step back
190 */
191

Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29

lines 192–194why, one step back
192app.use = function use(fn) {
193 var offset = 0;194 var path = '/';

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 195why, one step back
195

Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29

line 196why, one step back
196 // default path to '/'

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

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

Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29

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

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

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

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

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

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29

lines 212–214why, one step back
212 var fns = flatten(slice.call(arguments, offset));
213214 if (fns.length === 0) {

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 220why, one step back
220

Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29

lines 221–225why, one step back
221 fns.forEach(function (fn) {
222 // non-express app223 if (!fn || !fn.handle || !fn.set) {224 return router.use(path, fn);225 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 226why, one step back
226

Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29

lines 227–228why, one step back
227 debug('.use app under %s', path);
228 fn.mountpath = path;

Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4

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

Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29

line 230why, one step back
230

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 231why, one step back
231 // restore .app property on req and res

Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29

line 232why, one step back
232 router.use(path, function mounted_app(req, res, next) {

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 233why, one step back
233 var orig = req.app;

Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29

line 234why, one step back
234 fn.handle(req, res, function (err) {

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 237–241why, one step back
237 next(err);
238 });239 });240241 // mounted an app

Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 255why, one step back
255 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 269why, one step back
269 *

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

docs: visionmedia is now tj on GithubDouglas Christopher Wilsonbefore adoptione9539fc

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 286–292why, one step back
286 * library was created to map all of node's popular template
287 * engines to follow this convention, thus allowing them to288 * work seamlessly within Express.289 *290 * @param {String} ext291 * @param {Function} fn292 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 293why, one step back
293 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 296–308why, one step back
296app.engine = function engine(ext, fn) {
297 if (typeof fn !== 'function') {298 throw new Error('callback function required');299 }300301 // get file extension302 var extension = ext[0] !== '.'303 ? '.' + ext304 : ext;305306 // store engine307 this.engines[extension] = fn;308

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 309–320why, one step back
309 return this;
310};311312/**313 * Proxy to `Router#param()` with one added api feature. The _name_ parameter314 * can be an array of names.315 *316 * See the Router#param() docs for more details.317 *318 * @param {String|Array} name319 * @param {Function} fn320 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 321why, one step back
321 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 329why, one step back
329

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 334why, one step back
334

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 350why, one step back
350 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 356–366why, one step back
356 var settings = this.settings
357358 while (settings && settings !== Object.prototype) {359 if (hasOwnProperty.call(settings, setting)) {360 return settings[setting]361 }362363 settings = Object.getPrototypeOf(settings)364 }365366 return undefined

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 367–368why, one step back
367 }
368

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 371–377why, one step back
371 // set value
372 this.settings[setting] = val;373374 // trigger matched settings375 switch (setting) {376 case 'etag':377 this.set('etag fn', compileETag(val));

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

lines 378–380why, one step back
378 break;
379 case 'query parser':380 this.set('query parser fn', compileQueryParser(val));

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 381–383why, one step back
381 break;
382 case 'trust proxy':383 this.set('trust proxy fn', compileTrust(val));

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 384–390why, one step back
384
385 // trust proxy inherit back-compat386 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {387 configurable: true,388 value: false389 });390

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 391–407why, one step back
391 break;
392 }393394 return this;395};396397/**398 * Return the app's absolute pathname399 * based on the parent(s) that have400 * mounted it.401 *402 * For example if the application was403 * mounted as "/admin", which itself404 * was mounted as "/blog" then the405 * return value would be "/blog/admin".406 *407 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 408why, one step back
408 * @private

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 409–410why, one step back
409 */
410

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 412–428why, one step back
412 return this.parent
413 ? this.parent.path() + this.mountpath414 : '';415};416417/**418 * Check if `setting` is enabled (truthy).419 *420 * app.enabled('foo')421 * // => false422 *423 * app.enable('foo')424 * app.enabled('foo')425 * // => true426 *427 * @param {String} setting428 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 429why, one step back
429 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 430–431why, one step back
430 */
431

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 434–447why, one step back
434};
435436/**437 * Check if `setting` is disabled.438 *439 * app.disabled('foo')440 * // => true441 *442 * app.enable('foo')443 * app.disabled('foo')444 * // => false445 *446 * @param {String} setting447 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 448why, one step back
448 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 452–459why, one step back
452 return !this.set(setting);
453};454455/**456 * Enable `setting`.457 *458 * @param {String} setting459 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 460why, one step back
460 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 464–471why, one step back
464 return this.set(setting, true);
465};466467/**468 * Disable `setting`.469 *470 * @param {String} setting471 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 472why, one step back
472 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 473–474why, one step back
473 */
474

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 476–484why, one step back
476 return this.set(setting, false);
477};478479/**480 * Delegate `.VERB(...)` calls to `router.VERB(...)`.481 */482483methods.forEach(function(method){484 app[method] = function(path){

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 485–488why, one step back
485 if (method === 'get' && arguments.length === 1) {
486 // app.get(setting)487 return this.set(path);488 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 489why, one step back
489

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29

line 491why, one step back
491 route[method].apply(route, slice.call(arguments, 1));

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 492–502why, one step back
492 return this;
493 };494});495496/**497 * Special-cased "all" method, applying the given route `path`,498 * middleware, and callback to _every_ HTTP method.499 *500 * @param {String} path501 * @param {Function} ...502 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 503why, one step back
503 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 504–505why, one step back
504 */
505

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29

line 508why, one step back
508 var args = slice.call(arguments, 1);

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 509why, one step back
509

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 510–513why, one step back
510 for (var i = 0; i < methods.length; i++) {
511 route[methods[i]].apply(route, args);512 }513

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 514–528why, one step back
514 return this;
515};516517/**518 * Render the given view `name` name with `options`519 * and a callback accepting an error and the520 * rendered template string.521 *522 * Example:523 *524 * app.render('email', { name: 'Tobi' }, function(err, html){525 * // ...526 * })527 *528 * @param {String} name

Merge tag '4.13.4'Douglas Christopher Wilsonbefore adoption7cafdb5

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 530–531why, one step back
530 * @param {Function} callback
531 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 532–533why, one step back
532 */
533

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 538–539why, one step back
538 var opts = options;
539 var renderOptions = {};

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 540–542why, one step back
540 var view;
541542 // support callback function as second arg

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 543–545why, one step back
543 if (typeof options === 'function') {
544 done = options;545 opts = {};

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 546–548why, one step back
546 }
547548 // merge app.locals

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 550–551why, one step back
550
551 // merge options._locals

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 554why, one step back
554 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 555–556why, one step back
555
556 // merge options

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 558–559why, one step back
558
559 // set .cache unless explicitly provided

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 560–562why, one step back
560 if (renderOptions.cache == null) {
561 renderOptions.cache = this.enabled('view cache');562 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 563–564why, one step back
563
564 // primed cache

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 565–567why, one step back
565 if (renderOptions.cache) {
566 view = cache[name];567 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 568–570why, one step back
568
569 // view570 if (!view) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 571–573why, one step back
571 var View = this.get('view');
572573 view = new View(name, {

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 574–579why, one step back
574 defaultEngine: this.get('view engine'),
575 root: this.get('views'),576 engines: engines577 });578579 if (!view.path) {

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 586–588why, one step back
586 }
587588 // prime the cache

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 589–591why, one step back
589 if (renderOptions.cache) {
590 cache[name] = view;591 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 592–594why, one step back
592 }
593594 // render

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 596–615why, one step back
596};
597598/**599 * Listen for connections.600 *601 * A node `http.Server` is returned, with this602 * application (which is a `Function`) as its603 * callback. If you wish to create both an HTTP604 * and HTTPS server you may do so with the "http"605 * and "https" modules as shown here:606 *607 * var http = require('http')608 * , https = require('https')609 * , express = require('express')610 * , app = express();611 *612 * http.createServer(app).listen(80);613 * https.createServer({ ... }, app).listen(443);614 *615 * @return {http.Server}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 616why, one step back
616 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 619why, one step back
619app.listen = function listen() {

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 620–624why, one step back
620 var server = http.createServer(this);
621 return server.listen.apply(server, arguments);622};623624/**

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 625–629why, one step back
625 * Log error using console.error.
626 *627 * @param {Error} err628 * @private629 */

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 630why, one step back
630

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 634–646why, one step back
634}
635636/**637 * Try rendering a view.638 * @private639 */640641function tryRender(view, options, callback) {642 try {643 view.render(options, callback);644 } catch (err) {645 callback(err);646 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 647why, one step back
647}