application.js
This file
Path
lib/application.js
641 lines · 13.8 KiB · JavaScript
Last changed by
refactor: Remove Object.setPrototypeOf polyfill (#6081)Phillip Bartabefore adoption8cb53ea
History of this fileTake it
The walk stopped after 200 commits: lines older than e05a520 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.16.2'Douglas Christopher Wilsonbefore adoptione05a520
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.16.2'Douglas Christopher Wilsonbefore adoptione05a520
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;Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 24why, one step back24var merge = require('utils-merge');Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 25why, one step back25var resolve = require('path').resolve;call callback once on listen errorWesbefore adoptionee40a88
line 26why, one step back26var once = require('once')Use router module for routingDouglas Christopher Wilsonbefore adoptioncec5780
line 27why, one step back27var Router = require('router');Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 28–33why, one step back2829/**30 * Module variables.31 * @private32 */33Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 34why, one step back34var slice = Array.prototype.slice;Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
line 35why, one step back35var flatten = Array.prototype.flat;Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 36–43why, one step back3637/**38 * Application prototype.39 */4041var app = exports = module.exports = {};4243/**Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 44why, one step back44 * Variable for trust proxy inheritance back-compatMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 45why, one step back45 * @privateMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 46–50why, one step back46 */4748var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';4950/**Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 51–56why, one step back51 * Initialize the server.52 *53 * - setup default configuration54 * - setup default middleware55 * - setup route reflection methods56 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 57why, one step back57 * @privateMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 58–59why, one step back58 */59Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 60why, one step back60app.init = function init() {Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 61–62why, one step back61 var router = null;62Use object with null prototype for various app propertiesEvan Hahnbefore adoption1443973
lines 63–65why, one step back63 this.cache = Object.create(null);64 this.engines = Object.create(null);65 this.settings = Object.create(null);Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 66why, one step back66Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 67why, one step back67 this.defaultConfiguration();Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 68–83why, one step back6869 // Setup getting to lazily add base router70 Object.defineProperty(this, 'router', {71 configurable: true,72 enumerable: true,73 get: function getrouter() {74 if (router === null) {75 router = new Router({76 caseSensitive: this.enabled('case sensitive routing'),77 strict: this.enabled('strict routing')78 });79 }8081 return router;82 }83 });Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 84–87why, one step back84};8586/**87 * Initialize application configuration.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 88why, one step back88 * @privateMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 89–90why, one step back89 */90Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 91–93why, one step back91app.defaultConfiguration = function defaultConfiguration() {92 var env = process.env.NODE_ENV || 'development';93Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 94–97why, one step back94 // default settings95 this.enable('x-powered-by');96 this.set('etag', 'weak');97 this.set('env', env);Change query parser setting default to 'simple'Czarekbefore adoption450c468
line 98why, one step back98 this.set('query parser', 'simple')Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 99–101why, one step back99 this.set('subdomain offset', 2);100 this.set('trust proxy', false);101Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 102–107why, one step back102 // trust proxy inherit back-compat103 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {104 configurable: true,105 value: true106 });107Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 108–109why, one step back108 debug('booting in %s mode', env);109Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 110–118why, one step back110 this.on('mount', function onmount(parent) {111 // inherit trust proxy112 if (this.settings[trustProxyDefaultSymbol] === true113 && typeof parent.settings['trust proxy fn'] === 'function') {114 delete this.settings['trust proxy'];115 delete this.settings['trust proxy fn'];116 }117118 // inherit protosrefactor: Remove Object.setPrototypeOf polyfill (#6081)Phillip Bartabefore adoption8cb53ea
lines 119–122why, one step back119 Object.setPrototypeOf(this.request, parent.request)120 Object.setPrototypeOf(this.response, parent.response)121 Object.setPrototypeOf(this.engines, parent.engines)122 Object.setPrototypeOf(this.settings, parent.settings)Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 123–146why, one step back123 });124125 // setup locals126 this.locals = Object.create(null);127128 // top-most app is mounted at /129 this.mountpath = '/';130131 // default locals132 this.locals.settings = this.settings;133134 // default configuration135 this.set('view', View);136 this.set('views', resolve('views'));137 this.set('jsonp callback name', 'callback');138139 if (env === 'production') {140 this.enable('view cache');141 }142};143144/**145 * Dispatch a req, res pair into the application. Starts pipeline processing.146 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 147why, one step back147 * If no callback is provided, then default error handlers will respondMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 148–149why, one step back148 * in the event of an error bubbling through the stack.149 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 150why, one step back150 * @privateMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 151–152why, one step back151 */152Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 153why, one step back153app.handle = function handle(req, res, callback) {Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 154why, one step back154 // final handlerMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 155why, one step back155 var done = callback || finalhandler(req, res, {Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 156–159why, one step back156 env: this.get('env'),157 onerror: logerror.bind(this)158 });159Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054
lines 160–169why, one step back160 // set powered by header161 if (this.enabled('x-powered-by')) {162 res.setHeader('X-Powered-By', 'Express');163 }164165 // set circular references166 req.res = res;167 res.req = req;168169 // alter the prototypesrefactor: Remove Object.setPrototypeOf polyfill (#6081)Phillip Bartabefore adoption8cb53ea
lines 170–171why, one step back170 Object.setPrototypeOf(req, this.request)171 Object.setPrototypeOf(res, this.response)Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054
lines 172–175why, one step back172173 // setup locals174 if (!res.locals) {175 res.locals = Object.create(null);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 176–177why, one step back176 }177Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 178why, one step back178 this.router.handle(req, res, done);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 179–187why, one step back179};180181/**182 * Proxy `Router#use()` to add middleware to the app router.183 * See Router#use() documentation for details.184 *185 * If the _fn_ parameter is an express app, then it will be186 * mounted at the _route_ specified.187 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 188why, one step back188 * @publicMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 189–195why, one step back189 */190191app.use = function use(fn) {192 var offset = 0;193 var path = '/';194195 // default path to '/'Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
line 196why, one step back196 // disambiguate app.use([fn])Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 197why, one step back197 if (typeof fn !== 'function') {Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
lines 198–203why, one step back198 var arg = fn;199200 while (Array.isArray(arg) && arg.length !== 0) {201 arg = arg[0];202 }203Fix regression for empty string path in app.useDouglas Christopher Wilsonbefore adoption3c1a964
lines 204–205why, one step back204 // first arg is the path205 if (typeof arg !== 'function') {Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
lines 206–208why, one step back206 offset = 1;207 path = fn;208 }Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 209–210why, one step back209 }210Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
line 211why, one step back211 var fns = flatten.call(slice.call(arguments, offset), Infinity);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 212–213why, one step back212213 if (fns.length === 0) {Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 214why, one step back214 throw new TypeError('app.use() requires a middleware function')Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 215–216why, one step back215 }216Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 217–218why, one step back217 // get router218 var router = this.router;Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 219–227why, one step back219220 fns.forEach(function (fn) {221 // non-express app222 if (!fn || !fn.handle || !fn.set) {223 return router.use(path, fn);224 }225226 debug('.use app under %s', path);227 fn.mountpath = path;Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4
line 228why, one step back228 fn.parent = this;Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 229–233why, one step back229230 // restore .app property on req and res231 router.use(path, function mounted_app(req, res, next) {232 var orig = req.app;233 fn.handle(req, res, function (err) {refactor: Remove Object.setPrototypeOf polyfill (#6081)Phillip Bartabefore adoption8cb53ea
lines 234–235why, one step back234 Object.setPrototypeOf(req, orig.request)235 Object.setPrototypeOf(res, orig.response)Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 236–240why, one step back236 next(err);237 });238 });239240 // mounted an appPass context to .forEach instead of closurelemmybefore adoptiondfa7ee4
lines 241–242why, one step back241 fn.emit('mount', this);242 }, this);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 243–253why, one step back243244 return this;245};246247/**248 * Proxy to the app `Router#route()`249 * Returns a new `Route` instance for the _path_.250 *251 * Routes are isolated middleware stacks for specific paths.252 * See the Route api docs for details.253 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 254why, one step back254 * @publicMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 255–256why, one step back255 */256Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 257–258why, one step back257app.route = function route(path) {258 return this.router.route(path);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 259–266why, one step back259};260261/**262 * Register the given template engine callback `fn`263 * as `ext`.264 *265 * By default will `require()` the engine based on the266 * file extension. For example if you try to renderMerge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 267why, one step back267 * a "foo.ejs" file Express will invoke the following internally:Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 268why, one step back268 *Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 269why, one step back269 * app.engine('ejs', require('ejs').__express);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 270–280why, one step back270 *271 * For engines that do not provide `.__express` out of the box,272 * or if you wish to "map" a different extension to the template engine273 * you may use this method. For example mapping the EJS template engine to274 * ".html" files:275 *276 * app.engine('html', require('ejs').renderFile);277 *278 * In this case EJS provides a `.renderFile()` method with279 * the same signature that Express expects: `(path, options, callback)`,280 * though note that it aliases this method as `ejs.__express` internallyMerge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 281why, one step back281 * so if you're using ".ejs" extensions you don't need to do anything.Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 282–283why, one step back282 *283 * Some template engines do not follow this convention, thedocs: visionmedia is now tj on GithubDouglas Christopher Wilsonbefore adoptione9539fc
line 284why, one step back284 * [Consolidate.js](https://github.com/tj/consolidate.js)Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 285–291why, one step back285 * library was created to map all of node's popular template286 * engines to follow this convention, thus allowing them to287 * work seamlessly within Express.288 *289 * @param {String} ext290 * @param {Function} fn291 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 292why, one step back292 * @publicMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 293–294why, one step back293 */294Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 295–307why, one step back295app.engine = function engine(ext, fn) {296 if (typeof fn !== 'function') {297 throw new Error('callback function required');298 }299300 // get file extension301 var extension = ext[0] !== '.'302 ? '.' + ext303 : ext;304305 // store engine306 this.engines[extension] = fn;307Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 308–319why, one step back308 return this;309};310311/**312 * Proxy to `Router#param()` with one added api feature. The _name_ parameter313 * can be an array of names.314 *315 * See the Router#param() docs for more details.316 *317 * @param {String|Array} name318 * @param {Function} fn319 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 320why, one step back320 * @publicMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 321–322why, one step back321 */322Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 323why, one step back323app.param = function param(name, fn) {Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 324why, one step back324 if (Array.isArray(name)) {Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 325–327why, one step back325 for (var i = 0; i < name.length; i++) {326 this.param(name[i], fn);327 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 328why, one step back328Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 329–331why, one step back329 return this;330 }331Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 332why, one step back332 this.router.param(name, fn);Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 333why, one step back333Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 334–340why, one step back334 return this;335};336337/**338 * Assign `setting` to `val`, or return `setting`'s value.339 *340 * app.set('foo', 'bar');Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 341why, one step back341 * app.set('foo');Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 342–348why, one step back342 * // => "bar"343 *344 * Mounted servers inherit their parent server's settings.345 *346 * @param {String} setting347 * @param {*} [val]348 * @return {Server} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 349why, one step back349 * @publicMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 350–351why, one step back350 */351Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 352why, one step back352app.set = function set(setting, val) {Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 353–354why, one step back353 if (arguments.length === 1) {354 // app.get(setting)Use object with null prototype for settings closes #4835Evan Hahnbefore adoption26801a0
line 355why, one step back355 return this.settings[setting];Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 356–357why, one step back356 }357Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 358–359why, one step back358 debug('set "%s" to %o', setting, val);359Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 360–372why, one step back360 // set value361 this.settings[setting] = val;362363 // trigger matched settings364 switch (setting) {365 case 'etag':366 this.set('etag fn', compileETag(val));367 break;368 case 'query parser':369 this.set('query parser fn', compileQueryParser(val));370 break;371 case 'trust proxy':372 this.set('trust proxy fn', compileTrust(val));Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 373–379why, one step back373374 // trust proxy inherit back-compat375 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {376 configurable: true,377 value: false378 });379Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 380–396why, one step back380 break;381 }382383 return this;384};385386/**387 * Return the app's absolute pathname388 * based on the parent(s) that have389 * mounted it.390 *391 * For example if the application was392 * mounted as "/admin", which itself393 * was mounted as "/blog" then the394 * return value would be "/blog/admin".395 *396 * @return {String}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 397why, one step back397 * @privateMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 398–399why, one step back398 */399Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 400why, one step back400app.path = function path() {Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 401–417why, one step back401 return this.parent402 ? this.parent.path() + this.mountpath403 : '';404};405406/**407 * Check if `setting` is enabled (truthy).408 *409 * app.enabled('foo')410 * // => false411 *412 * app.enable('foo')413 * app.enabled('foo')414 * // => true415 *416 * @param {String} setting417 * @return {Boolean}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 418why, one step back418 * @publicMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 419–420why, one step back419 */420Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 421–422why, one step back421app.enabled = function enabled(setting) {422 return Boolean(this.set(setting));Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 423–436why, one step back423};424425/**426 * Check if `setting` is disabled.427 *428 * app.disabled('foo')429 * // => true430 *431 * app.enable('foo')432 * app.disabled('foo')433 * // => false434 *435 * @param {String} setting436 * @return {Boolean}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 437why, one step back437 * @publicMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 438–439why, one step back438 */439Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 440why, one step back440app.disabled = function disabled(setting) {Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 441–448why, one step back441 return !this.set(setting);442};443444/**445 * Enable `setting`.446 *447 * @param {String} setting448 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 449why, one step back449 * @publicMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 450–451why, one step back450 */451Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 452why, one step back452app.enable = function enable(setting) {Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 453–460why, one step back453 return this.set(setting, true);454};455456/**457 * Disable `setting`.458 *459 * @param {String} setting460 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 461why, one step back461 * @publicMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 462–463why, one step back462 */463Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 464why, one step back464app.disable = function disable(setting) {Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 465–473why, one step back465 return this.set(setting, false);466};467468/**469 * Delegate `.VERB(...)` calls to `router.VERB(...)`.470 */471472methods.forEach(function(method){473 app[method] = function(path){Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 474–477why, one step back474 if (method === 'get' && arguments.length === 1) {475 // app.get(setting)476 return this.set(path);477 }Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 478why, one step back478Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 479why, one step back479 var route = this.route(path);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 480–491why, one step back480 route[method].apply(route, slice.call(arguments, 1));481 return this;482 };483});484485/**486 * Special-cased "all" method, applying the given route `path`,487 * middleware, and callback to _every_ HTTP method.488 *489 * @param {String} path490 * @param {Function} ...491 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 492why, one step back492 * @publicMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 493–494why, one step back493 */494Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 495why, one step back495app.all = function all(path) {Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 496why, one step back496 var route = this.route(path);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 497–498why, one step back497 var args = slice.call(arguments, 1);498Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 499–502why, one step back499 for (var i = 0; i < methods.length; i++) {500 route[methods[i]].apply(route, args);501 }502Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 503–517why, one step back503 return this;504};505506/**507 * Render the given view `name` name with `options`508 * and a callback accepting an error and the509 * rendered template string.510 *511 * Example:512 *513 * app.render('email', { name: 'Tobi' }, function(err, html){514 * // ...515 * })516 *517 * @param {String} nameMerge tag '4.13.4'Douglas Christopher Wilsonbefore adoption7cafdb5
line 518why, one step back518 * @param {Object|Function} options or fnMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 519–520why, one step back519 * @param {Function} callback520 * @publicMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 521–522why, one step back521 */522Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 523why, one step back523app.render = function render(name, options, callback) {Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 524why, one step back524 var cache = this.cache;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 525why, one step back525 var done = callback;Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 526why, one step back526 var engines = this.engines;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 527–528why, one step back527 var opts = options;528 var renderOptions = {};Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 529–531why, one step back529 var view;530531 // support callback function as second argMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 532–534why, one step back532 if (typeof options === 'function') {533 done = options;534 opts = {};Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 535–537why, one step back535 }536537 // merge app.localsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 538why, one step back538 merge(renderOptions, this.locals);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 539–540why, one step back539540 // merge options._localsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 541–542why, one step back541 if (opts._locals) {542 merge(renderOptions, opts._locals);Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 543why, one step back543 }Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 544–545why, one step back544545 // merge optionsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 546why, one step back546 merge(renderOptions, opts);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 547–548why, one step back547548 // set .cache unless explicitly providedMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 549–551why, one step back549 if (renderOptions.cache == null) {550 renderOptions.cache = this.enabled('view cache');551 }Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 552–553why, one step back552553 // primed cacheMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 554–556why, one step back554 if (renderOptions.cache) {555 view = cache[name];556 }Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 557–559why, one step back557558 // view559 if (!view) {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 560–562why, one step back560 var View = this.get('view');561562 view = new View(name, {Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 563–568why, one step back563 defaultEngine: this.get('view engine'),564 root: this.get('views'),565 engines: engines566 });567568 if (!view.path) {Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 569–572why, one step back569 var dirs = Array.isArray(view.root) && view.root.length > 1570 ? 'directories "' + view.root.slice(0, -1).join('", "') + '" or "' + view.root[view.root.length - 1] + '"'571 : 'directory "' + view.root + '"'572 var err = new Error('Failed to lookup view "' + name + '" in views ' + dirs);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 573why, one step back573 err.view = view;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 574why, one step back574 return done(err);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 575–577why, one step back575 }576577 // prime the cacheMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 578–580why, one step back578 if (renderOptions.cache) {579 cache[name] = view;580 }Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 581–583why, one step back581 }582583 // renderMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 584why, one step back584 tryRender(view, renderOptions, done);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 585–604why, one step back585};586587/**588 * Listen for connections.589 *590 * A node `http.Server` is returned, with this591 * application (which is a `Function`) as its592 * callback. If you wish to create both an HTTP593 * and HTTPS server you may do so with the "http"594 * and "https" modules as shown here:595 *596 * var http = require('http')597 * , https = require('https')598 * , express = require('express')599 * , app = express();600 *601 * http.createServer(app).listen(80);602 * https.createServer({ ... }, app).listen(443);603 *604 * @return {http.Server}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 605why, one step back605 * @publicMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 606–607why, one step back606 */607call callback once on listen errorWesbefore adoptionee40a88
lines 608–616why, one step back608app.listen = function listen () {609 var server = http.createServer(this)610 var args = Array.prototype.slice.call(arguments)611 if (typeof args[args.length - 1] === 'function') {612 var done = args[args.length - 1] = once(args[args.length - 1])613 server.once('error', done)614 }615 return server.listen.apply(server, args)616}Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 617–618why, one step back617618/**Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 619–623why, one step back619 * Log error using console.error.620 *621 * @param {Error} err622 * @private623 */Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 624why, one step back624Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 625–626why, one step back625function logerror(err) {626 /* istanbul ignore next */Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 627why, one step back627 if (this.get('env') !== 'test') console.error(err.stack || err.toString());Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 628–640why, one step back628}629630/**631 * Try rendering a view.632 * @private633 */634635function tryRender(view, options, callback) {636 try {637 view.render(options, callback);638 } catch (err) {639 callback(err);640 }Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 641why, one step back641}