Skip to content
Myr

application.js

This file

Path

lib/application.js

630 lines · 13.5 KiB · JavaScript

Last changed by

deps: array-flatten@3.0.0Douglas Christopher Wilsonbefore adoption3d05e85

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

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 30–37why, one step back
30
31/**32 * Application prototype.33 */3435var app = exports = module.exports = {};3637/**

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 39why, one step back
39 * @private

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 40–44why, one step back
40 */
4142var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';4344/**

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 45–50why, one step back
45 * Initialize the server.
46 *47 * - setup default configuration48 * - setup default middleware49 * - setup route reflection methods50 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 51why, one step back
51 * @private

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 52–53why, one step back
52 */
53

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

lines 55–56why, one step back
55 var router = null;
56

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 57–58why, one step back
57 this.cache = {};
58 this.engines = {};

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 59–60why, one step back
59 this.settings = {};
60

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

lines 62–77why, one step back
62
63 // Setup getting to lazily add base router64 Object.defineProperty(this, 'router', {65 configurable: true,66 enumerable: true,67 get: function getrouter() {68 if (router === null) {69 router = new Router({70 caseSensitive: this.enabled('case sensitive routing'),71 strict: this.enabled('strict routing')72 });73 }7475 return router;76 }77 });

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 78–81why, one step back
78};
7980/**81 * Initialize application configuration.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 82why, one step back
82 * @private

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 83–84why, one step back
83 */
84

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 85–87why, one step back
85app.defaultConfiguration = function defaultConfiguration() {
86 var env = process.env.NODE_ENV || 'development';87

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 88–91why, one step back
88 // default settings
89 this.enable('x-powered-by');90 this.set('etag', 'weak');91 this.set('env', env);

Change query parser setting default to 'simple'Czarekbefore adoption450c468

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 93–95why, one step back
93 this.set('subdomain offset', 2);
94 this.set('trust proxy', false);95

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 96–101why, one step back
96 // trust proxy inherit back-compat
97 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {98 configurable: true,99 value: true100 });101

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 104–112why, one step back
104 this.on('mount', function onmount(parent) {
105 // inherit trust proxy106 if (this.settings[trustProxyDefaultSymbol] === true107 && typeof parent.settings['trust proxy fn'] === 'function') {108 delete this.settings['trust proxy'];109 delete this.settings['trust proxy fn'];110 }111112 // inherit protos

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

lines 113–116why, one step back
113 setPrototypeOf(this.request, parent.request)
114 setPrototypeOf(this.response, parent.response)115 setPrototypeOf(this.engines, parent.engines)116 setPrototypeOf(this.settings, parent.settings)

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 117–140why, one step back
117 });
118119 // setup locals120 this.locals = Object.create(null);121122 // top-most app is mounted at /123 this.mountpath = '/';124125 // default locals126 this.locals.settings = this.settings;127128 // default configuration129 this.set('view', View);130 this.set('views', resolve('views'));131 this.set('jsonp callback name', 'callback');132133 if (env === 'production') {134 this.enable('view cache');135 }136};137138/**139 * Dispatch a req, res pair into the application. Starts pipeline processing.140 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 144why, one step back
144 * @private

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 145–146why, one step back
145 */
146

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 150–153why, one step back
150 env: this.get('env'),
151 onerror: logerror.bind(this)152 });153

Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054

lines 154–163why, one step back
154 // set powered by header
155 if (this.enabled('x-powered-by')) {156 res.setHeader('X-Powered-By', 'Express');157 }158159 // set circular references160 req.res = res;161 res.req = req;162163 // alter the prototypes

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

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

Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054

lines 166–169why, one step back
166
167 // setup locals168 if (!res.locals) {169 res.locals = Object.create(null);

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 170–171why, one step back
170 }
171

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 173–181why, one step back
173};
174175/**176 * Proxy `Router#use()` to add middleware to the app router.177 * See Router#use() documentation for details.178 *179 * If the _fn_ parameter is an express app, then it will be180 * mounted at the _route_ specified.181 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 182why, one step back
182 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 183–184why, one step back
183 */
184

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

lines 185–187why, one step back
185app.use = function use(fn) {
186 var offset = 0;187 var path = '/';

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 188why, one step back
188

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

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

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

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

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

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

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

lines 192–197why, one step back
192 var arg = fn;
193194 while (Array.isArray(arg) && arg.length !== 0) {195 arg = arg[0];196 }197

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

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

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

lines 200–202why, one step back
200 offset = 1;
201 path = fn;202 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 203–204why, one step back
203 }
204

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

lines 205–207why, one step back
205 var fns = flatten(slice.call(arguments, offset));
206207 if (fns.length === 0) {

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

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

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

lines 211–212why, one step back
211 // get router
212 var router = this.router;

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 213why, one step back
213

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

lines 214–218why, one step back
214 fns.forEach(function (fn) {
215 // non-express app216 if (!fn || !fn.handle || !fn.set) {217 return router.use(path, fn);218 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 219why, one step back
219

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

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

Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4

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

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

line 223why, one step back
223

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

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

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

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 230–234why, one step back
230 next(err);
231 });232 });233234 // mounted an app

Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 237–247why, one step back
237
238 return this;239};240241/**242 * Proxy to the app `Router#route()`243 * Returns a new `Route` instance for the _path_.244 *245 * Routes are isolated middleware stacks for specific paths.246 * See the Route api docs for details.247 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 248why, one step back
248 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 249–250why, one step back
249 */
250

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 253–260why, one step back
253};
254255/**256 * Register the given template engine callback `fn`257 * as `ext`.258 *259 * By default will `require()` the engine based on the260 * file extension. For example if you try to render

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 262why, one step back
262 *

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

docs: visionmedia is now tj on GithubDouglas Christopher Wilsonbefore adoptione9539fc

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 279–285why, one step back
279 * library was created to map all of node's popular template
280 * engines to follow this convention, thus allowing them to281 * work seamlessly within Express.282 *283 * @param {String} ext284 * @param {Function} fn285 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 286why, one step back
286 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 287–288why, one step back
287 */
288

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 289–301why, one step back
289app.engine = function engine(ext, fn) {
290 if (typeof fn !== 'function') {291 throw new Error('callback function required');292 }293294 // get file extension295 var extension = ext[0] !== '.'296 ? '.' + ext297 : ext;298299 // store engine300 this.engines[extension] = fn;301

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 302–313why, one step back
302 return this;
303};304305/**306 * Proxy to `Router#param()` with one added api feature. The _name_ parameter307 * can be an array of names.308 *309 * See the Router#param() docs for more details.310 *311 * @param {String|Array} name312 * @param {Function} fn313 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 314why, one step back
314 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 315–316why, one step back
315 */
316

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 322why, one step back
322

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 323–325why, one step back
323 return this;
324 }325

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 327why, one step back
327

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 328–334why, one step back
328 return this;
329};330331/**332 * Assign `setting` to `val`, or return `setting`'s value.333 *334 * app.set('foo', 'bar');

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 336–342why, one step back
336 * // => "bar"
337 *338 * Mounted servers inherit their parent server's settings.339 *340 * @param {String} setting341 * @param {*} [val]342 * @return {Server} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 343why, one step back
343 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 344–345why, one step back
344 */
345

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 347–351why, one step back
347 if (arguments.length === 1) {
348 // app.get(setting)349 return this.settings[setting];350 }351

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 354–360why, one step back
354 // set value
355 this.settings[setting] = val;356357 // trigger matched settings358 switch (setting) {359 case 'etag':360 this.set('etag fn', compileETag(val));

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

lines 361–363why, one step back
361 break;
362 case 'query parser':363 this.set('query parser fn', compileQueryParser(val));

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 364–366why, one step back
364 break;
365 case 'trust proxy':366 this.set('trust proxy fn', compileTrust(val));

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 367–373why, one step back
367
368 // trust proxy inherit back-compat369 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {370 configurable: true,371 value: false372 });373

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 374–390why, one step back
374 break;
375 }376377 return this;378};379380/**381 * Return the app's absolute pathname382 * based on the parent(s) that have383 * mounted it.384 *385 * For example if the application was386 * mounted as "/admin", which itself387 * was mounted as "/blog" then the388 * return value would be "/blog/admin".389 *390 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 391why, one step back
391 * @private

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 392–393why, one step back
392 */
393

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 395–411why, one step back
395 return this.parent
396 ? this.parent.path() + this.mountpath397 : '';398};399400/**401 * Check if `setting` is enabled (truthy).402 *403 * app.enabled('foo')404 * // => false405 *406 * app.enable('foo')407 * app.enabled('foo')408 * // => true409 *410 * @param {String} setting411 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 412why, one step back
412 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 413–414why, one step back
413 */
414

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 417–430why, one step back
417};
418419/**420 * Check if `setting` is disabled.421 *422 * app.disabled('foo')423 * // => true424 *425 * app.enable('foo')426 * app.disabled('foo')427 * // => false428 *429 * @param {String} setting430 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 431why, one step back
431 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 432–433why, one step back
432 */
433

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 435–442why, one step back
435 return !this.set(setting);
436};437438/**439 * Enable `setting`.440 *441 * @param {String} setting442 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 443why, one step back
443 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 444–445why, one step back
444 */
445

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 447–454why, one step back
447 return this.set(setting, true);
448};449450/**451 * Disable `setting`.452 *453 * @param {String} setting454 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 455why, one step back
455 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 456–457why, one step back
456 */
457

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 459–467why, one step back
459 return this.set(setting, false);
460};461462/**463 * Delegate `.VERB(...)` calls to `router.VERB(...)`.464 */465466methods.forEach(function(method){467 app[method] = function(path){

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 468–471why, one step back
468 if (method === 'get' && arguments.length === 1) {
469 // app.get(setting)470 return this.set(path);471 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 472why, one step back
472

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 475–485why, one step back
475 return this;
476 };477});478479/**480 * Special-cased "all" method, applying the given route `path`,481 * middleware, and callback to _every_ HTTP method.482 *483 * @param {String} path484 * @param {Function} ...485 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 486why, one step back
486 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 487–488why, one step back
487 */
488

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

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 var args = slice.call(arguments, 1);

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 492why, one step back
492

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 493–496why, one step back
493 for (var i = 0; i < methods.length; i++) {
494 route[methods[i]].apply(route, args);495 }496

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 497–511why, one step back
497 return this;
498};499500/**501 * Render the given view `name` name with `options`502 * and a callback accepting an error and the503 * rendered template string.504 *505 * Example:506 *507 * app.render('email', { name: 'Tobi' }, function(err, html){508 * // ...509 * })510 *511 * @param {String} name

Merge tag '4.13.4'Douglas Christopher Wilsonbefore adoption7cafdb5

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 513–514why, one step back
513 * @param {Function} callback
514 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 515–516why, one step back
515 */
516

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 521–522why, one step back
521 var opts = options;
522 var renderOptions = {};

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 523–525why, one step back
523 var view;
524525 // support callback function as second arg

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 526–528why, one step back
526 if (typeof options === 'function') {
527 done = options;528 opts = {};

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 529–531why, one step back
529 }
530531 // merge app.locals

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 533–534why, one step back
533
534 // merge options._locals

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 537why, one step back
537 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 538–539why, one step back
538
539 // merge options

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 541–542why, one step back
541
542 // set .cache unless explicitly provided

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 543–545why, one step back
543 if (renderOptions.cache == null) {
544 renderOptions.cache = this.enabled('view cache');545 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 546–547why, one step back
546
547 // primed cache

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 548–550why, one step back
548 if (renderOptions.cache) {
549 view = cache[name];550 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 551–553why, one step back
551
552 // view553 if (!view) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 554–556why, one step back
554 var View = this.get('view');
555556 view = new View(name, {

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 557–562why, one step back
557 defaultEngine: this.get('view engine'),
558 root: this.get('views'),559 engines: engines560 });561562 if (!view.path) {

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 569–571why, one step back
569 }
570571 // prime the cache

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 572–574why, one step back
572 if (renderOptions.cache) {
573 cache[name] = view;574 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 575–577why, one step back
575 }
576577 // render

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 579–598why, one step back
579};
580581/**582 * Listen for connections.583 *584 * A node `http.Server` is returned, with this585 * application (which is a `Function`) as its586 * callback. If you wish to create both an HTTP587 * and HTTPS server you may do so with the "http"588 * and "https" modules as shown here:589 *590 * var http = require('http')591 * , https = require('https')592 * , express = require('express')593 * , app = express();594 *595 * http.createServer(app).listen(80);596 * https.createServer({ ... }, app).listen(443);597 *598 * @return {http.Server}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 599why, one step back
599 * @public

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 600–601why, one step back
600 */
601

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

lines 603–607why, one step back
603 var server = http.createServer(this);
604 return server.listen.apply(server, arguments);605};606607/**

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 608–612why, one step back
608 * Log error using console.error.
609 *610 * @param {Error} err611 * @private612 */

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 613why, one step back
613

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 617–629why, one step back
617}
618619/**620 * Try rendering a view.621 * @private622 */623624function tryRender(view, options, callback) {625 try {626 view.render(options, callback);627 } catch (err) {628 callback(err);629 }

deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405

line 630why, one step back
630}