application.js
This file
Path
lib/application.js
630 lines · 13.5 KiB · JavaScript
Last changed by
Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
History of this fileTake it
The walk stopped after 200 commits: lines older than fa6a405 point at it.
Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 1–8why, one step 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';10deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 11–12why, one step back11/**12 * Module dependencies.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 13why, one step back13 * @privatedeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 14–21why, 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;Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
line 22why, one step back22var compileQueryParser = require('./utils').compileQueryParser;deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 23why, one step back23var compileTrust = require('./utils').compileTrust;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 24why, one step back24var flatten = require('array-flatten');Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 25why, one step back25var merge = require('utils-merge');deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 26why, one step back26var resolve = require('path').resolve;Use router module for routingDouglas Christopher Wilsonbefore adoptioncec5780
line 27why, one step back27var Router = require('router');Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 28why, one step back28var setPrototypeOf = require('setprototypeof')Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 29why, one step back29var slice = Array.prototype.slice;deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 30–37why, one step back3031/**32 * Application prototype.33 */3435var app = exports = module.exports = {};3637/**Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 38why, one step back38 * Variable for trust proxy inheritance back-compatMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 39why, one step back39 * @privateMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 40–44why, one step back40 */4142var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';4344/**deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 45–50why, one step back45 * Initialize the server.46 *47 * - setup default configuration48 * - setup default middleware49 * - setup route reflection methods50 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 51why, one step back51 * @privatedeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 52–53why, one step back52 */53Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 54why, one step back54app.init = function init() {Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 55–56why, one step back55 var router = null;56deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 57–58why, one step back57 this.cache = {};58 this.engines = {};Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 59–60why, one step back59 this.settings = {};60deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 61why, one step back61 this.defaultConfiguration();Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 62–77why, one step back6263 // Setup getting to lazily add base router64 Object.defineProperty(this, 'router', {65 configurable: true,66 enumerable: true,67 get: function getrouter() {68 if (router === null) {69 router = new Router({70 caseSensitive: this.enabled('case sensitive routing'),71 strict: this.enabled('strict routing')72 });73 }7475 return router;76 }77 });deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 78–81why, one step back78};7980/**81 * Initialize application configuration.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 82why, one step back82 * @privatedeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 83–84why, one step back83 */84Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 85–87why, one step back85app.defaultConfiguration = function defaultConfiguration() {86 var env = process.env.NODE_ENV || 'development';87deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 88–91why, one step back88 // default settings89 this.enable('x-powered-by');90 this.set('etag', 'weak');91 this.set('env', env);Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
line 92why, one step back92 this.set('query parser', 'extended');deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 93–95why, one step back93 this.set('subdomain offset', 2);94 this.set('trust proxy', false);95Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 96–101why, one step back96 // trust proxy inherit back-compat97 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {98 configurable: true,99 value: true100 });101deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 102–103why, one step back102 debug('booting in %s mode', env);103Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 104–112why, one step back104 this.on('mount', function onmount(parent) {105 // inherit trust proxy106 if (this.settings[trustProxyDefaultSymbol] === true107 && typeof parent.settings['trust proxy fn'] === 'function') {108 delete this.settings['trust proxy'];109 delete this.settings['trust proxy fn'];110 }111112 // inherit protosMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
lines 113–116why, one step back113 setPrototypeOf(this.request, parent.request)114 setPrototypeOf(this.response, parent.response)115 setPrototypeOf(this.engines, parent.engines)116 setPrototypeOf(this.settings, parent.settings)deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 117–140why, one step back117 });118119 // setup locals120 this.locals = Object.create(null);121122 // top-most app is mounted at /123 this.mountpath = '/';124125 // default locals126 this.locals.settings = this.settings;127128 // default configuration129 this.set('view', View);130 this.set('views', resolve('views'));131 this.set('jsonp callback name', 'callback');132133 if (env === 'production') {134 this.enable('view cache');135 }136};137138/**139 * Dispatch a req, res pair into the application. Starts pipeline processing.140 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 141why, one step back141 * If no callback is provided, then default error handlers will responddeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 142–143why, one step back142 * in the event of an error bubbling through the stack.143 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 144why, one step back144 * @privatedeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 145–146why, one step back145 */146Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 147why, one step back147app.handle = function handle(req, res, callback) {deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 148why, one step back148 // final handlerMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 149why, one step back149 var done = callback || finalhandler(req, res, {deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 150–153why, one step back150 env: this.get('env'),151 onerror: logerror.bind(this)152 });153Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054
lines 154–163why, one step back154 // set powered by header155 if (this.enabled('x-powered-by')) {156 res.setHeader('X-Powered-By', 'Express');157 }158159 // set circular references160 req.res = res;161 res.req = req;162163 // alter the prototypesMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
lines 164–165why, one step back164 setPrototypeOf(req, this.request)165 setPrototypeOf(res, this.response)Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054
lines 166–169why, one step back166167 // setup locals168 if (!res.locals) {169 res.locals = Object.create(null);deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 170–171why, one step back170 }171Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 172why, one step back172 this.router.handle(req, res, done);deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 173–181why, one step back173};174175/**176 * Proxy `Router#use()` to add middleware to the app router.177 * See Router#use() documentation for details.178 *179 * If the _fn_ parameter is an express app, then it will be180 * mounted at the _route_ specified.181 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 182why, one step back182 * @publicdeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 183–184why, one step back183 */184Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
lines 185–187why, one step back185app.use = function use(fn) {186 var offset = 0;187 var path = '/';deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 188why, one step back188Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 189why, one step back189 // default path to '/'Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
line 190why, one step back190 // disambiguate app.use([fn])Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 191why, one step back191 if (typeof fn !== 'function') {Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
lines 192–197why, one step back192 var arg = fn;193194 while (Array.isArray(arg) && arg.length !== 0) {195 arg = arg[0];196 }197Fix regression for empty string path in app.useDouglas Christopher Wilsonbefore adoption3c1a964
lines 198–199why, one step back198 // first arg is the path199 if (typeof arg !== 'function') {Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
lines 200–202why, one step back200 offset = 1;201 path = fn;202 }deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 203–204why, one step back203 }204Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
lines 205–207why, one step back205 var fns = flatten(slice.call(arguments, offset));206207 if (fns.length === 0) {Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 208why, one step back208 throw new TypeError('app.use() requires a middleware function')Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
lines 209–210why, one step back209 }210Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 211–212why, one step back211 // get router212 var router = this.router;deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 213why, one step back213Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
lines 214–218why, one step back214 fns.forEach(function (fn) {215 // non-express app216 if (!fn || !fn.handle || !fn.set) {217 return router.use(path, fn);218 }deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 219why, one step back219Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
lines 220–221why, one step back220 debug('.use app under %s', path);221 fn.mountpath = path;Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4
line 222why, one step back222 fn.parent = this;Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 223why, one step back223deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 224why, one step back224 // restore .app property on req and resSupport mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 225why, one step back225 router.use(path, function mounted_app(req, res, next) {deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 226why, one step back226 var orig = req.app;Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 227why, one step back227 fn.handle(req, res, function (err) {Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
lines 228–229why, one step back228 setPrototypeOf(req, orig.request)229 setPrototypeOf(res, orig.response)deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 230–234why, one step back230 next(err);231 });232 });233234 // mounted an appPass context to .forEach instead of closurelemmybefore adoptiondfa7ee4
lines 235–236why, one step back235 fn.emit('mount', this);236 }, this);deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 237–247why, one step back237238 return this;239};240241/**242 * Proxy to the app `Router#route()`243 * Returns a new `Route` instance for the _path_.244 *245 * Routes are isolated middleware stacks for specific paths.246 * See the Route api docs for details.247 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 248why, one step back248 * @publicdeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 249–250why, one step back249 */250Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 251–252why, one step back251app.route = function route(path) {252 return this.router.route(path);deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 253–260why, one step back253};254255/**256 * Register the given template engine callback `fn`257 * as `ext`.258 *259 * By default will `require()` the engine based on the260 * file extension. For example if you try to renderMerge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 261why, one step back261 * a "foo.ejs" file Express will invoke the following internally:deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 262why, one step back262 *Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 263why, one step back263 * app.engine('ejs', require('ejs').__express);deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 264–277why, one step back264 *265 * For engines that do not provide `.__express` out of the box,266 * or if you wish to "map" a different extension to the template engine267 * you may use this method. For example mapping the EJS template engine to268 * ".html" files:269 *270 * app.engine('html', require('ejs').renderFile);271 *272 * In this case EJS provides a `.renderFile()` method with273 * the same signature that Express expects: `(path, options, callback)`,274 * though note that it aliases this method as `ejs.__express` internally275 * so if you're using ".ejs" extensions you dont need to do anything.276 *277 * Some template engines do not follow this convention, thedocs: visionmedia is now tj on GithubDouglas Christopher Wilsonbefore adoptione9539fc
line 278why, one step back278 * [Consolidate.js](https://github.com/tj/consolidate.js)deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 279–285why, one step back279 * library was created to map all of node's popular template280 * engines to follow this convention, thus allowing them to281 * work seamlessly within Express.282 *283 * @param {String} ext284 * @param {Function} fn285 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 286why, one step back286 * @publicdeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 287–288why, one step back287 */288Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 289–301why, one step back289app.engine = function engine(ext, fn) {290 if (typeof fn !== 'function') {291 throw new Error('callback function required');292 }293294 // get file extension295 var extension = ext[0] !== '.'296 ? '.' + ext297 : ext;298299 // store engine300 this.engines[extension] = fn;301deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 302–313why, one step back302 return this;303};304305/**306 * Proxy to `Router#param()` with one added api feature. The _name_ parameter307 * can be an array of names.308 *309 * See the Router#param() docs for more details.310 *311 * @param {String|Array} name312 * @param {Function} fn313 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 314why, one step back314 * @publicdeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 315–316why, one step back315 */316Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 317why, one step back317app.param = function param(name, fn) {deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 318why, one step back318 if (Array.isArray(name)) {Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 319–321why, one step back319 for (var i = 0; i < name.length; i++) {320 this.param(name[i], fn);321 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 322why, one step back322deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 323–325why, one step back323 return this;324 }325Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 326why, one step back326 this.router.param(name, fn);Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 327why, one step back327deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 328–334why, one step back328 return this;329};330331/**332 * Assign `setting` to `val`, or return `setting`'s value.333 *334 * app.set('foo', 'bar');Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 335why, one step back335 * app.set('foo');deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 336–342why, one step back336 * // => "bar"337 *338 * Mounted servers inherit their parent server's settings.339 *340 * @param {String} setting341 * @param {*} [val]342 * @return {Server} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 343why, one step back343 * @publicdeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 344–345why, one step back344 */345Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 346why, one step back346app.set = function set(setting, val) {deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 347–351why, one step back347 if (arguments.length === 1) {348 // app.get(setting)349 return this.settings[setting];350 }351Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 352–353why, one step back352 debug('set "%s" to %o', setting, val);353deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 354–360why, one step back354 // set value355 this.settings[setting] = val;356357 // trigger matched settings358 switch (setting) {359 case 'etag':360 this.set('etag fn', compileETag(val));Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 361–363why, one step back361 break;362 case 'query parser':363 this.set('query parser fn', compileQueryParser(val));deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 364–366why, one step back364 break;365 case 'trust proxy':366 this.set('trust proxy fn', compileTrust(val));Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 367–373why, one step back367368 // trust proxy inherit back-compat369 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {370 configurable: true,371 value: false372 });373deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 374–390why, one step back374 break;375 }376377 return this;378};379380/**381 * Return the app's absolute pathname382 * based on the parent(s) that have383 * mounted it.384 *385 * For example if the application was386 * mounted as "/admin", which itself387 * was mounted as "/blog" then the388 * return value would be "/blog/admin".389 *390 * @return {String}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 391why, one step back391 * @privatedeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 392–393why, one step back392 */393Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 394why, one step back394app.path = function path() {deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 395–411why, one step back395 return this.parent396 ? this.parent.path() + this.mountpath397 : '';398};399400/**401 * Check if `setting` is enabled (truthy).402 *403 * app.enabled('foo')404 * // => false405 *406 * app.enable('foo')407 * app.enabled('foo')408 * // => true409 *410 * @param {String} setting411 * @return {Boolean}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 412why, one step back412 * @publicdeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 413–414why, one step back413 */414Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 415–416why, one step back415app.enabled = function enabled(setting) {416 return Boolean(this.set(setting));deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 417–430why, one step back417};418419/**420 * Check if `setting` is disabled.421 *422 * app.disabled('foo')423 * // => true424 *425 * app.enable('foo')426 * app.disabled('foo')427 * // => false428 *429 * @param {String} setting430 * @return {Boolean}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 431why, one step back431 * @publicdeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 432–433why, one step back432 */433Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 434why, one step back434app.disabled = function disabled(setting) {deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 435–442why, one step back435 return !this.set(setting);436};437438/**439 * Enable `setting`.440 *441 * @param {String} setting442 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 443why, one step back443 * @publicdeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 444–445why, one step back444 */445Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 446why, one step back446app.enable = function enable(setting) {deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 447–454why, one step back447 return this.set(setting, true);448};449450/**451 * Disable `setting`.452 *453 * @param {String} setting454 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 455why, one step back455 * @publicdeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 456–457why, one step back456 */457Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 458why, one step back458app.disable = function disable(setting) {deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 459–467why, one step back459 return this.set(setting, false);460};461462/**463 * Delegate `.VERB(...)` calls to `router.VERB(...)`.464 */465466methods.forEach(function(method){467 app[method] = function(path){Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 468–471why, one step back468 if (method === 'get' && arguments.length === 1) {469 // app.get(setting)470 return this.set(path);471 }deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 472why, one step back472Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 473why, one step back473 var route = this.route(path);Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 474why, one step back474 route[method].apply(route, slice.call(arguments, 1));deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 475–485why, one step back475 return this;476 };477});478479/**480 * Special-cased "all" method, applying the given route `path`,481 * middleware, and callback to _every_ HTTP method.482 *483 * @param {String} path484 * @param {Function} ...485 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 486why, one step back486 * @publicdeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 487–488why, one step back487 */488Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 489why, one step back489app.all = function all(path) {Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 490why, one step back490 var route = this.route(path);Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 491why, one step back491 var args = slice.call(arguments, 1);deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 492why, one step back492Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 493–496why, one step back493 for (var i = 0; i < methods.length; i++) {494 route[methods[i]].apply(route, args);495 }496deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 497–511why, one step back497 return this;498};499500/**501 * Render the given view `name` name with `options`502 * and a callback accepting an error and the503 * rendered template string.504 *505 * Example:506 *507 * app.render('email', { name: 'Tobi' }, function(err, html){508 * // ...509 * })510 *511 * @param {String} nameMerge tag '4.13.4'Douglas Christopher Wilsonbefore adoption7cafdb5
line 512why, one step back512 * @param {Object|Function} options or fnMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 513–514why, one step back513 * @param {Function} callback514 * @publicdeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 515–516why, one step back515 */516Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 517why, one step back517app.render = function render(name, options, callback) {deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 518why, one step back518 var cache = this.cache;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 519why, one step back519 var done = callback;deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 520why, one step back520 var engines = this.engines;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 521–522why, one step back521 var opts = options;522 var renderOptions = {};deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 523–525why, one step back523 var view;524525 // support callback function as second argMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 526–528why, one step back526 if (typeof options === 'function') {527 done = options;528 opts = {};deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 529–531why, one step back529 }530531 // merge app.localsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 532why, one step back532 merge(renderOptions, this.locals);deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 533–534why, one step back533534 // merge options._localsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 535–536why, one step back535 if (opts._locals) {536 merge(renderOptions, opts._locals);Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 537why, one step back537 }deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 538–539why, one step back538539 // merge optionsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 540why, one step back540 merge(renderOptions, opts);deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 541–542why, one step back541542 // set .cache unless explicitly providedMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 543–545why, one step back543 if (renderOptions.cache == null) {544 renderOptions.cache = this.enabled('view cache');545 }deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 546–547why, one step back546547 // primed cacheMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 548–550why, one step back548 if (renderOptions.cache) {549 view = cache[name];550 }deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 551–553why, one step back551552 // view553 if (!view) {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 554–556why, one step back554 var View = this.get('view');555556 view = new View(name, {deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 557–562why, one step back557 defaultEngine: this.get('view engine'),558 root: this.get('views'),559 engines: engines560 });561562 if (!view.path) {Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 563–566why, one step back563 var dirs = Array.isArray(view.root) && view.root.length > 1564 ? 'directories "' + view.root.slice(0, -1).join('", "') + '" or "' + view.root[view.root.length - 1] + '"'565 : 'directory "' + view.root + '"'566 var err = new Error('Failed to lookup view "' + name + '" in views ' + dirs);deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 567why, one step back567 err.view = view;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 568why, one step back568 return done(err);deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 569–571why, one step back569 }570571 // prime the cacheMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 572–574why, one step back572 if (renderOptions.cache) {573 cache[name] = view;574 }deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 575–577why, one step back575 }576577 // renderMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 578why, one step back578 tryRender(view, renderOptions, done);deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 579–598why, one step back579};580581/**582 * Listen for connections.583 *584 * A node `http.Server` is returned, with this585 * application (which is a `Function`) as its586 * callback. If you wish to create both an HTTP587 * and HTTPS server you may do so with the "http"588 * and "https" modules as shown here:589 *590 * var http = require('http')591 * , https = require('https')592 * , express = require('express')593 * , app = express();594 *595 * http.createServer(app).listen(80);596 * https.createServer({ ... }, app).listen(443);597 *598 * @return {http.Server}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 599why, one step back599 * @publicdeps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 600–601why, one step back600 */601Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 602why, one step back602app.listen = function listen() {deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
lines 603–607why, one step back603 var server = http.createServer(this);604 return server.listen.apply(server, arguments);605};606607/**Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 608–612why, one step back608 * Log error using console.error.609 *610 * @param {Error} err611 * @private612 */deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 613why, one step back613Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 614–615why, one step back614function logerror(err) {615 /* istanbul ignore next */deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 616why, one step back616 if (this.get('env') !== 'test') console.error(err.stack || err.toString());Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 617–629why, one step back617}618619/**620 * Try rendering a view.621 * @private622 */623624function tryRender(view, options, callback) {625 try {626 view.render(options, callback);627 } catch (err) {628 callback(err);629 }deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionfa6a405
line 630why, one step back630}