application.js
This file
Path
lib/application.js
642 lines · 13.8 KiB · JavaScript
Last changed by
call callback once on listen errorWesbefore adoptionee40a88
History of this fileTake it
The walk stopped after 200 commits: lines older than d8237b9 point at it.
Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 1–8why, one step back1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2013 Roman Shtylman5 * Copyright(c) 2014-2015 Douglas Christopher Wilson6 * MIT Licensed7 */8Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 9–10why, one step back9'use strict';10Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 11–12why, one step back11/**12 * Module dependencies.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 13why, one step back13 * @privateMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 14–23why, one step back14 */1516var finalhandler = require('finalhandler');17var methods = require('methods');18var debug = require('debug')('express:application');19var View = require('./view');20var http = require('http');21var compileETag = require('./utils').compileETag;22var compileQueryParser = require('./utils').compileQueryParser;23var compileTrust = require('./utils').compileTrust;deps: array-flatten@3.0.0Douglas Christopher Wilsonbefore adoption3d05e85
line 24why, one step back24var flatten = require('array-flatten').flattenMerge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 25why, one step back25var merge = require('utils-merge');Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 26why, one step back26var resolve = require('path').resolve;call callback once on listen errorWesbefore adoptionee40a88
line 27why, one step back27var once = require('once')Use router module for routingDouglas Christopher Wilsonbefore adoptioncec5780
line 28why, one step back28var Router = require('router');Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 29why, one step back29var setPrototypeOf = require('setprototypeof')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 30–35why, one step back3031/**32 * Module variables.33 * @private34 */35Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 36why, one step back36var slice = Array.prototype.slice;Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 37–44why, one step back3738/**39 * Application prototype.40 */4142var app = exports = module.exports = {};4344/**Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 45why, one step back45 * Variable for trust proxy inheritance back-compatMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 46why, one step back46 * @privateMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 47–51why, one step back47 */4849var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';5051/**Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 52–57why, one step back52 * 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 back58 * @privateMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 59–60why, one step back59 */60Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 61why, one step back61app.init = function init() {Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 62–63why, one step back62 var router = null;63Use object with null prototype for various app propertiesEvan Hahnbefore adoption1443973
lines 64–66why, one step back64 this.cache = Object.create(null);65 this.engines = Object.create(null);66 this.settings = Object.create(null);Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 67why, one step back67Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 68why, one step back68 this.defaultConfiguration();Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 69–84why, one step back6970 // 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 });Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 85–88why, one step back85};8687/**88 * Initialize application configuration.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 89why, one step back89 * @privateMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 90–91why, one step back90 */91Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 92–94why, one step back92app.defaultConfiguration = function defaultConfiguration() {93 var env = process.env.NODE_ENV || 'development';94Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 95–98why, one step back95 // default settings96 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 back99 this.set('query parser', 'simple')Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 100–102why, one step back100 this.set('subdomain offset', 2);101 this.set('trust proxy', false);102Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 103–108why, one step back103 // trust proxy inherit back-compat104 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {105 configurable: true,106 value: true107 });108Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 109–110why, one step back109 debug('booting in %s mode', env);110Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 111–119why, one step back111 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 protosMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
lines 120–123why, one step back120 setPrototypeOf(this.request, parent.request)121 setPrototypeOf(this.response, parent.response)122 setPrototypeOf(this.engines, parent.engines)123 setPrototypeOf(this.settings, parent.settings)Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 124–147why, one step back124 });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 back148 * If no callback is provided, then default error handlers will respondMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 149–150why, one step back149 * in the event of an error bubbling through the stack.150 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 151why, one step back151 * @privateMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 152–153why, one step back152 */153Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 154why, one step back154app.handle = function handle(req, res, callback) {Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 155why, one step back155 // final handlerMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 156why, one step back156 var done = callback || finalhandler(req, res, {Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 157–160why, one step back157 env: this.get('env'),158 onerror: logerror.bind(this)159 });160Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054
lines 161–170why, one step back161 // set powered by header162 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 prototypesMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
lines 171–172why, one step back171 setPrototypeOf(req, this.request)172 setPrototypeOf(res, this.response)Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054
lines 173–176why, one step back173174 // setup locals175 if (!res.locals) {176 res.locals = Object.create(null);Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 177–178why, one step back177 }178Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 179why, one step back179 this.router.handle(req, res, done);Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 180–188why, one step back180};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 back189 * @publicMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 190–191why, one step back190 */191Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
lines 192–194why, one step back192app.use = function use(fn) {193 var offset = 0;194 var path = '/';Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 195why, one step back195Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 196why, one step back196 // default path to '/'Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
line 197why, one step back197 // disambiguate app.use([fn])Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 198why, one step back198 if (typeof fn !== 'function') {Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
lines 199–204why, one step back199 var arg = fn;200201 while (Array.isArray(arg) && arg.length !== 0) {202 arg = arg[0];203 }204Fix regression for empty string path in app.useDouglas Christopher Wilsonbefore adoption3c1a964
lines 205–206why, one step back205 // first arg is the path206 if (typeof arg !== 'function') {Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
lines 207–209why, one step back207 offset = 1;208 path = fn;209 }Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 210–211why, one step back210 }211Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
lines 212–214why, one step back212 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 back215 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 back216 }217Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 218–219why, one step back218 // get router219 var router = this.router;Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 220why, one step back220Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
lines 221–225why, one step back221 fns.forEach(function (fn) {222 // non-express app223 if (!fn || !fn.handle || !fn.set) {224 return router.use(path, fn);225 }Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 226why, one step back226Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
lines 227–228why, one step back227 debug('.use app under %s', path);228 fn.mountpath = path;Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4
line 229why, one step back229 fn.parent = this;Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 230why, one step back230Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 231why, one step back231 // restore .app property on req and resSupport mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 232why, one step back232 router.use(path, function mounted_app(req, res, next) {Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 233why, one step back233 var orig = req.app;Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 234why, one step back234 fn.handle(req, res, function (err) {Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
lines 235–236why, one step back235 setPrototypeOf(req, orig.request)236 setPrototypeOf(res, orig.response)Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 237–241why, one step back237 next(err);238 });239 });240241 // mounted an appPass context to .forEach instead of closurelemmybefore adoptiondfa7ee4
lines 242–243why, one step back242 fn.emit('mount', this);243 }, this);Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 244–254why, one step back244245 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 back255 * @publicMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 256–257why, one step back256 */257Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 258–259why, one step back258app.route = function route(path) {259 return this.router.route(path);Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 260–267why, one step back260};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 renderMerge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 268why, one step back268 * a "foo.ejs" file Express will invoke the following internally:Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 269why, one step back269 *Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 270why, one step back270 * app.engine('ejs', require('ejs').__express);Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 271–281why, one step back271 *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` internallyMerge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 282why, one step back282 * so if you're using ".ejs" extensions you don't need to do anything.Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 283–284why, one step back283 *284 * Some template engines do not follow this convention, thedocs: visionmedia is now tj on GithubDouglas Christopher Wilsonbefore adoptione9539fc
line 285why, one step back285 * [Consolidate.js](https://github.com/tj/consolidate.js)Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 286–292why, one step back286 * library was created to map all of node's popular template287 * engines to follow this convention, thus allowing them to288 * work seamlessly within Express.289 *290 * @param {String} ext291 * @param {Function} fn292 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 293why, one step back293 * @publicMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 294–295why, one step back294 */295Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 296–308why, one step back296app.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;308Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 309–320why, one step back309 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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 321why, one step back321 * @publicMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 322–323why, one step back322 */323Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 324why, one step back324app.param = function param(name, fn) {Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 325why, one step back325 if (Array.isArray(name)) {Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 326–328why, one step back326 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 back329Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 330–332why, one step back330 return this;331 }332Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 333why, one step back333 this.router.param(name, fn);Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 334why, one step back334Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 335–341why, one step back335 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 back342 * app.set('foo');Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 343–349why, one step back343 * // => "bar"344 *345 * Mounted servers inherit their parent server's settings.346 *347 * @param {String} setting348 * @param {*} [val]349 * @return {Server} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 350why, one step back350 * @publicMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 351–352why, one step back351 */352Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 353why, one step back353app.set = function set(setting, val) {Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 354–355why, one step back354 if (arguments.length === 1) {355 // app.get(setting)Use object with null prototype for settings closes #4835Evan Hahnbefore adoption26801a0
line 356why, one step back356 return this.settings[setting];Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 357–358why, one step back357 }358Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 359–360why, one step back359 debug('set "%s" to %o', setting, val);360Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 361–373why, one step back361 // set value362 this.settings[setting] = val;363364 // trigger matched settings365 switch (setting) {366 case 'etag':367 this.set('etag fn', compileETag(val));368 break;369 case 'query parser':370 this.set('query parser fn', compileQueryParser(val));371 break;372 case 'trust proxy':373 this.set('trust proxy fn', compileTrust(val));Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 374–380why, one step back374375 // trust proxy inherit back-compat376 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {377 configurable: true,378 value: false379 });380Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 381–397why, one step back381 break;382 }383384 return this;385};386387/**388 * Return the app's absolute pathname389 * based on the parent(s) that have390 * mounted it.391 *392 * For example if the application was393 * mounted as "/admin", which itself394 * was mounted as "/blog" then the395 * return value would be "/blog/admin".396 *397 * @return {String}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 398why, one step back398 * @privateMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 399–400why, one step back399 */400Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 401why, one step back401app.path = function path() {Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 402–418why, one step back402 return this.parent403 ? this.parent.path() + this.mountpath404 : '';405};406407/**408 * Check if `setting` is enabled (truthy).409 *410 * app.enabled('foo')411 * // => false412 *413 * app.enable('foo')414 * app.enabled('foo')415 * // => true416 *417 * @param {String} setting418 * @return {Boolean}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 419why, one step back419 * @publicMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 420–421why, one step back420 */421Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 422–423why, one step back422app.enabled = function enabled(setting) {423 return Boolean(this.set(setting));Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 424–437why, one step back424};425426/**427 * Check if `setting` is disabled.428 *429 * app.disabled('foo')430 * // => true431 *432 * app.enable('foo')433 * app.disabled('foo')434 * // => false435 *436 * @param {String} setting437 * @return {Boolean}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 438why, one step back438 * @publicMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 439–440why, one step back439 */440Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 441why, one step back441app.disabled = function disabled(setting) {Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 442–449why, one step back442 return !this.set(setting);443};444445/**446 * Enable `setting`.447 *448 * @param {String} setting449 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 450why, one step back450 * @publicMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 451–452why, one step back451 */452Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 453why, one step back453app.enable = function enable(setting) {Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 454–461why, one step back454 return this.set(setting, true);455};456457/**458 * Disable `setting`.459 *460 * @param {String} setting461 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 462why, one step back462 * @publicMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 463–464why, one step back463 */464Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 465why, one step back465app.disable = function disable(setting) {Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 466–474why, one step back466 return this.set(setting, false);467};468469/**470 * Delegate `.VERB(...)` calls to `router.VERB(...)`.471 */472473methods.forEach(function(method){474 app[method] = function(path){Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 475–478why, one step back475 if (method === 'get' && arguments.length === 1) {476 // app.get(setting)477 return this.set(path);478 }Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 479why, one step back479Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 480why, one step back480 var route = this.route(path);Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 481why, one step back481 route[method].apply(route, slice.call(arguments, 1));Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 482–492why, one step back482 return this;483 };484});485486/**487 * Special-cased "all" method, applying the given route `path`,488 * middleware, and callback to _every_ HTTP method.489 *490 * @param {String} path491 * @param {Function} ...492 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 493why, one step back493 * @publicMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 494–495why, one step back494 */495Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 496why, one step back496app.all = function all(path) {Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 497why, one step back497 var route = this.route(path);Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 498why, one step back498 var args = slice.call(arguments, 1);Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 499why, one step back499Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 500–503why, one step back500 for (var i = 0; i < methods.length; i++) {501 route[methods[i]].apply(route, args);502 }503Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 504–518why, one step back504 return this;505};506507/**508 * Render the given view `name` name with `options`509 * and a callback accepting an error and the510 * rendered template string.511 *512 * Example:513 *514 * app.render('email', { name: 'Tobi' }, function(err, html){515 * // ...516 * })517 *518 * @param {String} nameMerge tag '4.13.4'Douglas Christopher Wilsonbefore adoption7cafdb5
line 519why, one step back519 * @param {Object|Function} options or fnMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 520–521why, one step back520 * @param {Function} callback521 * @publicMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 522–523why, one step back522 */523Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 524why, one step back524app.render = function render(name, options, callback) {Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 525why, one step back525 var cache = this.cache;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 526why, one step back526 var done = callback;Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 527why, one step back527 var engines = this.engines;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 528–529why, one step back528 var opts = options;529 var renderOptions = {};Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 530–532why, one step back530 var view;531532 // support callback function as second argMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 533–535why, one step back533 if (typeof options === 'function') {534 done = options;535 opts = {};Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 536–538why, one step back536 }537538 // merge app.localsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 539why, one step back539 merge(renderOptions, this.locals);Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 540–541why, one step back540541 // merge options._localsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 542–543why, one step back542 if (opts._locals) {543 merge(renderOptions, opts._locals);Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 544why, one step back544 }Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 545–546why, one step back545546 // merge optionsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 547why, one step back547 merge(renderOptions, opts);Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 548–549why, one step back548549 // set .cache unless explicitly providedMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 550–552why, one step back550 if (renderOptions.cache == null) {551 renderOptions.cache = this.enabled('view cache');552 }Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 553–554why, one step back553554 // primed cacheMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 555–557why, one step back555 if (renderOptions.cache) {556 view = cache[name];557 }Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 558–560why, one step back558559 // view560 if (!view) {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 561–563why, one step back561 var View = this.get('view');562563 view = new View(name, {Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 564–569why, one step back564 defaultEngine: this.get('view engine'),565 root: this.get('views'),566 engines: engines567 });568569 if (!view.path) {Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 570–573why, one step back570 var dirs = Array.isArray(view.root) && view.root.length > 1571 ? 'directories "' + view.root.slice(0, -1).join('", "') + '" or "' + view.root[view.root.length - 1] + '"'572 : 'directory "' + view.root + '"'573 var err = new Error('Failed to lookup view "' + name + '" in views ' + dirs);Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 574why, one step back574 err.view = view;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 575why, one step back575 return done(err);Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 576–578why, one step back576 }577578 // prime the cacheMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 579–581why, one step back579 if (renderOptions.cache) {580 cache[name] = view;581 }Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 582–584why, one step back582 }583584 // renderMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 585why, one step back585 tryRender(view, renderOptions, done);Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 586–605why, one step back586};587588/**589 * Listen for connections.590 *591 * A node `http.Server` is returned, with this592 * application (which is a `Function`) as its593 * callback. If you wish to create both an HTTP594 * and HTTPS server you may do so with the "http"595 * and "https" modules as shown here:596 *597 * var http = require('http')598 * , https = require('https')599 * , express = require('express')600 * , app = express();601 *602 * http.createServer(app).listen(80);603 * https.createServer({ ... }, app).listen(443);604 *605 * @return {http.Server}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 606why, one step back606 * @publicMerge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 607–608why, one step back607 */608call callback once on listen errorWesbefore adoptionee40a88
lines 609–617why, one step back609app.listen = function listen () {610 var server = http.createServer(this)611 var args = Array.prototype.slice.call(arguments)612 if (typeof args[args.length - 1] === 'function') {613 var done = args[args.length - 1] = once(args[args.length - 1])614 server.once('error', done)615 }616 return server.listen.apply(server, args)617}Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
lines 618–619why, one step back618619/**Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 620–624why, one step back620 * Log error using console.error.621 *622 * @param {Error} err623 * @private624 */Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 625why, one step back625Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 626–627why, one step back626function logerror(err) {627 /* istanbul ignore next */Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 628why, one step back628 if (this.get('env') !== 'test') console.error(err.stack || err.toString());Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 629–641why, one step back629}630631/**632 * Try rendering a view.633 * @private634 */635636function tryRender(view, options, callback) {637 try {638 view.render(options, callback);639 } catch (err) {640 callback(err);641 }Merge tag '3.15.3' into 4.7.xDouglas Christopher Wilsonbefore adoptiond8237b9
line 642why, one step back642}