Skip to content
Myr

application.js

This file

Path

lib/application.js

629 lines · 13.4 KiB · JavaScript

Last changed by

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

History of this file

Take it

The walk stopped after 200 commits: lines older than 3136e98 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: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 25why, one step back
25var flatten = require('array-flatten');

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

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

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 29–36why, one step back
29
30/**31 * Application prototype.32 */3334var app = exports = module.exports = {};3536/**

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 38why, one step back
38 * @private

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 39–43why, one step back
39 */
4041var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';4243/**

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 44–49why, one step back
44 * Initialize the server.
45 *46 * - setup default configuration47 * - setup default middleware48 * - setup route reflection methods49 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 50why, one step back
50 * @private

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 77–80why, one step back
77};
7879/**80 * Initialize application configuration.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 81why, one step back
81 * @private

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

line 91why, one step back
91 this.set('query parser', 'extended');

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 103–111why, one step back
103 this.on('mount', function onmount(parent) {
104 // inherit trust proxy105 if (this.settings[trustProxyDefaultSymbol] === true106 && typeof parent.settings['trust proxy fn'] === 'function') {107 delete this.settings['trust proxy'];108 delete this.settings['trust proxy fn'];109 }110111 // inherit protos

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 112–139why, one step back
112 this.request.__proto__ = parent.request;
113 this.response.__proto__ = parent.response;114 this.engines.__proto__ = parent.engines;115 this.settings.__proto__ = parent.settings;116 });117118 // setup locals119 this.locals = Object.create(null);120121 // top-most app is mounted at /122 this.mountpath = '/';123124 // default locals125 this.locals.settings = this.settings;126127 // default configuration128 this.set('view', View);129 this.set('views', resolve('views'));130 this.set('jsonp callback name', 'callback');131132 if (env === 'production') {133 this.enable('view cache');134 }135};136137/**138 * Dispatch a req, res pair into the application. Starts pipeline processing.139 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 143why, one step back
143 * @private

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054

lines 153–168why, one step back
153 // set powered by header
154 if (this.enabled('x-powered-by')) {155 res.setHeader('X-Powered-By', 'Express');156 }157158 // set circular references159 req.res = res;160 res.req = req;161162 // alter the prototypes163 req.__proto__ = this.request;164 res.__proto__ = this.response;165166 // setup locals167 if (!res.locals) {168 res.locals = Object.create(null);

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 181why, one step back
181 * @public

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

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

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 187why, one step back
187

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

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

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

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

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

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

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

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

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

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

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

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

Support non-string path in app.use(path, fn)Douglas Christopher Wilsonbefore adoption1c3bd36

line 202why, one step back
202 }

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 203why, one step back
203

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

lines 204–209why, one step back
204 var fns = flatten(slice.call(arguments, offset));
205206 if (fns.length === 0) {207 throw new TypeError('app.use() requires middleware functions');208 }209

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 212why, one step back
212

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

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

Accept multiple callbacks to app.use()Douglas Christopher Wilsonbefore adoption997a558

line 218why, one step back
218

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

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

Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4

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

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

line 222why, one step back
222

Accept multiple callbacks to app.use()Douglas Christopher Wilsonbefore adoption997a558

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

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

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

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

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 227–230why, one step back
227 req.__proto__ = orig.request;
228 res.__proto__ = orig.response;229 next(err);230 });

Accept multiple callbacks to app.use()Douglas Christopher Wilsonbefore adoption997a558

line 231why, one step back
231 });

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 232why, one step back
232

Accept multiple callbacks to app.use()Douglas Christopher Wilsonbefore adoption997a558

line 233why, one step back
233 // mounted an app

Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4

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

Accept multiple callbacks to app.use()Douglas Christopher Wilsonbefore adoption997a558

line 236why, one step back
236

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 247why, one step back
247 * @public

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 252–276why, one step back
252};
253254/**255 * Register the given template engine callback `fn`256 * as `ext`.257 *258 * By default will `require()` the engine based on the259 * file extension. For example if you try to render260 * a "foo.jade" file Express will invoke the following internally:261 *262 * app.engine('jade', require('jade').__express);263 *264 * For engines that do not provide `.__express` out of the box,265 * or if you wish to "map" a different extension to the template engine266 * you may use this method. For example mapping the EJS template engine to267 * ".html" files:268 *269 * app.engine('html', require('ejs').renderFile);270 *271 * In this case EJS provides a `.renderFile()` method with272 * the same signature that Express expects: `(path, options, callback)`,273 * though note that it aliases this method as `ejs.__express` internally274 * so if you're using ".ejs" extensions you dont need to do anything.275 *276 * Some template engines do not follow this convention, the

docs: visionmedia is now tj on GithubDouglas Christopher Wilsonbefore adoptione9539fc

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 278–284why, one step back
278 * library was created to map all of node's popular template
279 * engines to follow this convention, thus allowing them to280 * work seamlessly within Express.281 *282 * @param {String} ext283 * @param {Function} fn284 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 285why, one step back
285 * @public

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 288–300why, one step back
288app.engine = function engine(ext, fn) {
289 if (typeof fn !== 'function') {290 throw new Error('callback function required');291 }292293 // get file extension294 var extension = ext[0] !== '.'295 ? '.' + ext296 : ext;297298 // store engine299 this.engines[extension] = fn;300

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 301–312why, one step back
301 return this;
302};303304/**305 * Proxy to `Router#param()` with one added api feature. The _name_ parameter306 * can be an array of names.307 *308 * See the Router#param() docs for more details.309 *310 * @param {String|Array} name311 * @param {Function} fn312 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 313why, one step back
313 * @public

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 321why, one step back
321

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 326why, one step back
326

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 327–341why, one step back
327 return this;
328};329330/**331 * Assign `setting` to `val`, or return `setting`'s value.332 *333 * app.set('foo', 'bar');334 * app.get('foo');335 * // => "bar"336 *337 * Mounted servers inherit their parent server's settings.338 *339 * @param {String} setting340 * @param {*} [val]341 * @return {Server} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 342why, one step back
342 * @public

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 366–372why, one step back
366
367 // trust proxy inherit back-compat368 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {369 configurable: true,370 value: false371 });372

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 373–389why, one step back
373 break;
374 }375376 return this;377};378379/**380 * Return the app's absolute pathname381 * based on the parent(s) that have382 * mounted it.383 *384 * For example if the application was385 * mounted as "/admin", which itself386 * was mounted as "/blog" then the387 * return value would be "/blog/admin".388 *389 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 390why, one step back
390 * @private

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 394–410why, one step back
394 return this.parent
395 ? this.parent.path() + this.mountpath396 : '';397};398399/**400 * Check if `setting` is enabled (truthy).401 *402 * app.enabled('foo')403 * // => false404 *405 * app.enable('foo')406 * app.enabled('foo')407 * // => true408 *409 * @param {String} setting410 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 411why, one step back
411 * @public

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 416–429why, one step back
416};
417418/**419 * Check if `setting` is disabled.420 *421 * app.disabled('foo')422 * // => true423 *424 * app.enable('foo')425 * app.disabled('foo')426 * // => false427 *428 * @param {String} setting429 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 430why, one step back
430 * @public

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 442why, one step back
442 * @public

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 454why, one step back
454 * @public

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 458–466why, one step back
458 return this.set(setting, false);
459};460461/**462 * Delegate `.VERB(...)` calls to `router.VERB(...)`.463 */464465methods.forEach(function(method){466 app[method] = function(path){

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 471why, one step back
471

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

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

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 485why, one step back
485 * @public

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1

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

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

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 491why, one step back
491

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 522–524why, one step back
522 var view;
523524 // support callback function as second arg

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 528–530why, one step back
528 }
529530 // merge app.locals

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 536why, one step back
536 }

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 553–555why, one step back
553 var View = this.get('view');
554555 view = new View(name, {

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 556–561why, one step back
556 defaultEngine: this.get('view engine'),
557 root: this.get('views'),558 engines: engines559 });560561 if (!view.path) {

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

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 568–570why, one step back
568 }
569570 // prime the cache

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 574–576why, one step back
574 }
575576 // render

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 598why, one step back
598 * @public

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 607–611why, one step back
607 * Log error using console.error.
608 *609 * @param {Error} err610 * @private611 */

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 612why, one step back
612

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 616–628why, one step back
616}
617618/**619 * Try rendering a view.620 * @private621 */622623function tryRender(view, options, callback) {624 try {625 view.render(options, callback);626 } catch (err) {627 callback(err);628 }

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 629why, one step back
629}