application.js
This file
Path
lib/application.js
629 lines · 13.4 KiB · JavaScript
Last changed by
Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
History of this fileTake it
The walk stopped after 200 commits: lines older than 3136e98 point at it.
Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 1–8why, one step 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: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 11–12why, one step back11/**12 * Module dependencies.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 13why, one step back13 * @privatedeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 14–22why, one step back14 */1516var finalhandler = require('finalhandler');17var Router = require('./router');18var methods = require('methods');19var debug = require('debug')('express:application');20var View = require('./view');21var http = require('http');22var compileETag = require('./utils').compileETag;Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
line 23why, one step back23var compileQueryParser = require('./utils').compileQueryParser;deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 24why, one step back24var compileTrust = require('./utils').compileTrust;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 25why, one step back25var flatten = require('array-flatten');Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 26why, one step back26var merge = require('utils-merge');deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 27why, one step back27var resolve = require('path').resolve;Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 28why, one step back28var slice = Array.prototype.slice;deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 29–36why, one step back2930/**31 * Application prototype.32 */3334var app = exports = module.exports = {};3536/**Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 37why, one step back37 * Variable for trust proxy inheritance back-compatMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 38why, one step back38 * @privateMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 39–43why, one step back39 */4041var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';4243/**deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 44–49why, one step back44 * Initialize the server.45 *46 * - setup default configuration47 * - setup default middleware48 * - setup route reflection methods49 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 50why, one step back50 * @privatedeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 51–52why, one step back51 */52Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 53why, one step back53app.init = function init() {Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 54–55why, one step back54 var router = null;55deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 56–57why, one step back56 this.cache = {};57 this.engines = {};Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 58–59why, one step back58 this.settings = {};59deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 60why, one step back60 this.defaultConfiguration();Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 61–76why, one step back6162 // Setup getting to lazily add base router63 Object.defineProperty(this, 'router', {64 configurable: true,65 enumerable: true,66 get: function getrouter() {67 if (router === null) {68 router = new Router({69 caseSensitive: this.enabled('case sensitive routing'),70 strict: this.enabled('strict routing')71 });72 }7374 return router;75 }76 });deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 77–80why, one step back77};7879/**80 * Initialize application configuration.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 81why, one step back81 * @privatedeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 82–83why, one step back82 */83Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 84–86why, one step back84app.defaultConfiguration = function defaultConfiguration() {85 var env = process.env.NODE_ENV || 'development';86deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 87–90why, one step back87 // default settings88 this.enable('x-powered-by');89 this.set('etag', 'weak');90 this.set('env', env);Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
line 91why, one step back91 this.set('query parser', 'extended');deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 92–94why, one step back92 this.set('subdomain offset', 2);93 this.set('trust proxy', false);94Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 95–100why, one step back95 // trust proxy inherit back-compat96 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {97 configurable: true,98 value: true99 });100deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 101–102why, one step back101 debug('booting in %s mode', env);102Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 103–111why, one step back103 this.on('mount', function onmount(parent) {104 // inherit trust proxy105 if (this.settings[trustProxyDefaultSymbol] === true106 && typeof parent.settings['trust proxy fn'] === 'function') {107 delete this.settings['trust proxy'];108 delete this.settings['trust proxy fn'];109 }110111 // inherit protosdeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 112–139why, one step back112 this.request.__proto__ = parent.request;113 this.response.__proto__ = parent.response;114 this.engines.__proto__ = parent.engines;115 this.settings.__proto__ = parent.settings;116 });117118 // setup locals119 this.locals = Object.create(null);120121 // top-most app is mounted at /122 this.mountpath = '/';123124 // default locals125 this.locals.settings = this.settings;126127 // default configuration128 this.set('view', View);129 this.set('views', resolve('views'));130 this.set('jsonp callback name', 'callback');131132 if (env === 'production') {133 this.enable('view cache');134 }135};136137/**138 * Dispatch a req, res pair into the application. Starts pipeline processing.139 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 140why, one step back140 * If no callback is provided, then default error handlers will responddeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 141–142why, one step back141 * in the event of an error bubbling through the stack.142 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 143why, one step back143 * @privatedeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 144–145why, one step back144 */145Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 146why, one step back146app.handle = function handle(req, res, callback) {deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 147why, one step back147 // final handlerMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 148why, one step back148 var done = callback || finalhandler(req, res, {deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 149–152why, one step back149 env: this.get('env'),150 onerror: logerror.bind(this)151 });152Refactor away init middlewareDouglas Christopher Wilsonbefore adoption78e5054
lines 153–168why, one step back153 // set powered by header154 if (this.enabled('x-powered-by')) {155 res.setHeader('X-Powered-By', 'Express');156 }157158 // set circular references159 req.res = res;160 res.req = req;161162 // alter the prototypes163 req.__proto__ = this.request;164 res.__proto__ = this.response;165166 // setup locals167 if (!res.locals) {168 res.locals = Object.create(null);deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 169–170why, one step back169 }170Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 171why, one step back171 this.router.handle(req, res, done);deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 172–180why, one step back172};173174/**175 * Proxy `Router#use()` to add middleware to the app router.176 * See Router#use() documentation for details.177 *178 * If the _fn_ parameter is an express app, then it will be179 * mounted at the _route_ specified.180 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 181why, one step back181 * @publicdeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 182–183why, one step back182 */183Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
lines 184–186why, one step back184app.use = function use(fn) {185 var offset = 0;186 var path = '/';deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 187why, one step back187Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 188why, one step back188 // default path to '/'Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
line 189why, one step back189 // disambiguate app.use([fn])Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 190why, one step back190 if (typeof fn !== 'function') {Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
lines 191–196why, one step back191 var arg = fn;192193 while (Array.isArray(arg) && arg.length !== 0) {194 arg = arg[0];195 }196Fix regression for empty string path in app.useDouglas Christopher Wilsonbefore adoption3c1a964
lines 197–198why, one step back197 // first arg is the path198 if (typeof arg !== 'function') {Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
lines 199–201why, one step back199 offset = 1;200 path = fn;201 }Support non-string path in app.use(path, fn)Douglas Christopher Wilsonbefore adoption1c3bd36
line 202why, one step back202 }deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 203why, one step back203Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
lines 204–209why, one step back204 var fns = flatten(slice.call(arguments, offset));205206 if (fns.length === 0) {207 throw new TypeError('app.use() requires middleware functions');208 }209Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 210–211why, one step back210 // get router211 var router = this.router;deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 212why, one step back212Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
lines 213–217why, one step back213 fns.forEach(function (fn) {214 // non-express app215 if (!fn || !fn.handle || !fn.set) {216 return router.use(path, fn);217 }Accept multiple callbacks to app.use()Douglas Christopher Wilsonbefore adoption997a558
line 218why, one step back218Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
lines 219–220why, one step back219 debug('.use app under %s', path);220 fn.mountpath = path;Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4
line 221why, one step back221 fn.parent = this;Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 222why, one step back222Accept multiple callbacks to app.use()Douglas Christopher Wilsonbefore adoption997a558
line 223why, one step back223 // restore .app property on req and resSupport mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 224why, one step back224 router.use(path, function mounted_app(req, res, next) {deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 225why, one step back225 var orig = req.app;Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 226why, one step back226 fn.handle(req, res, function (err) {deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 227–230why, one step back227 req.__proto__ = orig.request;228 res.__proto__ = orig.response;229 next(err);230 });Accept multiple callbacks to app.use()Douglas Christopher Wilsonbefore adoption997a558
line 231why, one step back231 });deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 232why, one step back232Accept multiple callbacks to app.use()Douglas Christopher Wilsonbefore adoption997a558
line 233why, one step back233 // mounted an appPass context to .forEach instead of closurelemmybefore adoptiondfa7ee4
lines 234–235why, one step back234 fn.emit('mount', this);235 }, this);Accept multiple callbacks to app.use()Douglas Christopher Wilsonbefore adoption997a558
line 236why, one step back236deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 237–246why, one step back237 return this;238};239240/**241 * Proxy to the app `Router#route()`242 * Returns a new `Route` instance for the _path_.243 *244 * Routes are isolated middleware stacks for specific paths.245 * See the Route api docs for details.246 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 247why, one step back247 * @publicdeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 248–249why, one step back248 */249Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 250–251why, one step back250app.route = function route(path) {251 return this.router.route(path);deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 252–276why, one step back252};253254/**255 * Register the given template engine callback `fn`256 * as `ext`.257 *258 * By default will `require()` the engine based on the259 * file extension. For example if you try to render260 * a "foo.jade" file Express will invoke the following internally:261 *262 * app.engine('jade', require('jade').__express);263 *264 * For engines that do not provide `.__express` out of the box,265 * or if you wish to "map" a different extension to the template engine266 * you may use this method. For example mapping the EJS template engine to267 * ".html" files:268 *269 * app.engine('html', require('ejs').renderFile);270 *271 * In this case EJS provides a `.renderFile()` method with272 * the same signature that Express expects: `(path, options, callback)`,273 * though note that it aliases this method as `ejs.__express` internally274 * so if you're using ".ejs" extensions you dont need to do anything.275 *276 * Some template engines do not follow this convention, thedocs: visionmedia is now tj on GithubDouglas Christopher Wilsonbefore adoptione9539fc
line 277why, one step back277 * [Consolidate.js](https://github.com/tj/consolidate.js)deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 278–284why, one step back278 * library was created to map all of node's popular template279 * engines to follow this convention, thus allowing them to280 * work seamlessly within Express.281 *282 * @param {String} ext283 * @param {Function} fn284 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 285why, one step back285 * @publicdeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 286–287why, one step back286 */287Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 288–300why, one step back288app.engine = function engine(ext, fn) {289 if (typeof fn !== 'function') {290 throw new Error('callback function required');291 }292293 // get file extension294 var extension = ext[0] !== '.'295 ? '.' + ext296 : ext;297298 // store engine299 this.engines[extension] = fn;300deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 301–312why, one step back301 return this;302};303304/**305 * Proxy to `Router#param()` with one added api feature. The _name_ parameter306 * can be an array of names.307 *308 * See the Router#param() docs for more details.309 *310 * @param {String|Array} name311 * @param {Function} fn312 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 313why, one step back313 * @publicdeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 314–315why, one step back314 */315Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 316why, one step back316app.param = function param(name, fn) {deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 317why, one step back317 if (Array.isArray(name)) {Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
lines 318–320why, one step back318 for (var i = 0; i < name.length; i++) {319 this.param(name[i], fn);320 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 321why, one step back321deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 322–324why, one step back322 return this;323 }324Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 325why, one step back325 this.router.param(name, fn);Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 326why, one step back326deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 327–341why, one step back327 return this;328};329330/**331 * Assign `setting` to `val`, or return `setting`'s value.332 *333 * app.set('foo', 'bar');334 * app.get('foo');335 * // => "bar"336 *337 * Mounted servers inherit their parent server's settings.338 *339 * @param {String} setting340 * @param {*} [val]341 * @return {Server} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 342why, one step back342 * @publicdeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 343–344why, one step back343 */344Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 345why, one step back345app.set = function set(setting, val) {deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 346–350why, one step back346 if (arguments.length === 1) {347 // app.get(setting)348 return this.settings[setting];349 }350Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 351–352why, one step back351 debug('set "%s" to %o', setting, val);352deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 353–359why, one step back353 // set value354 this.settings[setting] = val;355356 // trigger matched settings357 switch (setting) {358 case 'etag':359 this.set('etag fn', compileETag(val));Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 360–362why, one step back360 break;361 case 'query parser':362 this.set('query parser fn', compileQueryParser(val));deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 363–365why, one step back363 break;364 case 'trust proxy':365 this.set('trust proxy fn', compileTrust(val));Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 366–372why, one step back366367 // trust proxy inherit back-compat368 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {369 configurable: true,370 value: false371 });372deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 373–389why, one step back373 break;374 }375376 return this;377};378379/**380 * Return the app's absolute pathname381 * based on the parent(s) that have382 * mounted it.383 *384 * For example if the application was385 * mounted as "/admin", which itself386 * was mounted as "/blog" then the387 * return value would be "/blog/admin".388 *389 * @return {String}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 390why, one step back390 * @privatedeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 391–392why, one step back391 */392Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 393why, one step back393app.path = function path() {deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 394–410why, one step back394 return this.parent395 ? this.parent.path() + this.mountpath396 : '';397};398399/**400 * Check if `setting` is enabled (truthy).401 *402 * app.enabled('foo')403 * // => false404 *405 * app.enable('foo')406 * app.enabled('foo')407 * // => true408 *409 * @param {String} setting410 * @return {Boolean}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 411why, one step back411 * @publicdeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 412–413why, one step back412 */413Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 414–415why, one step back414app.enabled = function enabled(setting) {415 return Boolean(this.set(setting));deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 416–429why, one step back416};417418/**419 * Check if `setting` is disabled.420 *421 * app.disabled('foo')422 * // => true423 *424 * app.enable('foo')425 * app.disabled('foo')426 * // => false427 *428 * @param {String} setting429 * @return {Boolean}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 430why, one step back430 * @publicdeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 431–432why, one step back431 */432Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 433why, one step back433app.disabled = function disabled(setting) {deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 434–441why, one step back434 return !this.set(setting);435};436437/**438 * Enable `setting`.439 *440 * @param {String} setting441 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 442why, one step back442 * @publicdeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 443–444why, one step back443 */444Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 445why, one step back445app.enable = function enable(setting) {deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 446–453why, one step back446 return this.set(setting, true);447};448449/**450 * Disable `setting`.451 *452 * @param {String} setting453 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 454why, one step back454 * @publicdeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 455–456why, one step back455 */456Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 457why, one step back457app.disable = function disable(setting) {deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 458–466why, one step back458 return this.set(setting, false);459};460461/**462 * Delegate `.VERB(...)` calls to `router.VERB(...)`.463 */464465methods.forEach(function(method){466 app[method] = function(path){Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 467–470why, one step back467 if (method === 'get' && arguments.length === 1) {468 // app.get(setting)469 return this.set(path);470 }deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 471why, one step back471Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 472why, one step back472 var route = this.route(path);Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 473why, one step back473 route[method].apply(route, slice.call(arguments, 1));deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 474–484why, one step back474 return this;475 };476});477478/**479 * Special-cased "all" method, applying the given route `path`,480 * middleware, and callback to _every_ HTTP method.481 *482 * @param {String} path483 * @param {Function} ...484 * @return {app} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 485why, one step back485 * @publicdeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 486–487why, one step back486 */487Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 488why, one step back488app.all = function all(path) {Add app.router reference backDouglas Christopher Wilsonbefore adoption509ebb1
line 489why, one step back489 var route = this.route(path);Support mounted app as any argument to app.use()Fabien Franzenbefore adoption17cea29
line 490why, one step back490 var args = slice.call(arguments, 1);deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 491why, one step back491Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 492–495why, one step back492 for (var i = 0; i < methods.length; i++) {493 route[methods[i]].apply(route, args);494 }495deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 496–511why, one step back496 return this;497};498499/**500 * Render the given view `name` name with `options`501 * and a callback accepting an error and the502 * rendered template string.503 *504 * Example:505 *506 * app.render('email', { name: 'Tobi' }, function(err, html){507 * // ...508 * })509 *510 * @param {String} name511 * @param {String|Function} options or fnMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 512–513why, one step back512 * @param {Function} callback513 * @publicdeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 514–515why, one step back514 */515Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 516why, one step back516app.render = function render(name, options, callback) {deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 517why, one step back517 var cache = this.cache;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 518why, one step back518 var done = callback;deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 519why, one step back519 var engines = this.engines;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 520–521why, one step back520 var opts = options;521 var renderOptions = {};deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 522–524why, one step back522 var view;523524 // support callback function as second argMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 525–527why, one step back525 if (typeof options === 'function') {526 done = options;527 opts = {};deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 528–530why, one step back528 }529530 // merge app.localsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 531why, one step back531 merge(renderOptions, this.locals);deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 532–533why, one step back532533 // merge options._localsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 534–535why, one step back534 if (opts._locals) {535 merge(renderOptions, opts._locals);Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 536why, one step back536 }deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 537–538why, one step back537538 // merge optionsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 539why, one step back539 merge(renderOptions, opts);deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 540–541why, one step back540541 // set .cache unless explicitly providedMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 542–544why, one step back542 if (renderOptions.cache == null) {543 renderOptions.cache = this.enabled('view cache');544 }deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 545–546why, one step back545546 // primed cacheMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 547–549why, one step back547 if (renderOptions.cache) {548 view = cache[name];549 }deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 550–552why, one step back550551 // view552 if (!view) {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 553–555why, one step back553 var View = this.get('view');554555 view = new View(name, {deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 556–561why, one step back556 defaultEngine: this.get('view engine'),557 root: this.get('views'),558 engines: engines559 });560561 if (!view.path) {Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 562–565why, one step back562 var dirs = Array.isArray(view.root) && view.root.length > 1563 ? 'directories "' + view.root.slice(0, -1).join('", "') + '" or "' + view.root[view.root.length - 1] + '"'564 : 'directory "' + view.root + '"'565 var err = new Error('Failed to lookup view "' + name + '" in views ' + dirs);deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 566why, one step back566 err.view = view;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 567why, one step back567 return done(err);deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 568–570why, one step back568 }569570 // prime the cacheMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 571–573why, one step back571 if (renderOptions.cache) {572 cache[name] = view;573 }deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 574–576why, one step back574 }575576 // renderMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 577why, one step back577 tryRender(view, renderOptions, done);deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 578–597why, one step back578};579580/**581 * Listen for connections.582 *583 * A node `http.Server` is returned, with this584 * application (which is a `Function`) as its585 * callback. If you wish to create both an HTTP586 * and HTTPS server you may do so with the "http"587 * and "https" modules as shown here:588 *589 * var http = require('http')590 * , https = require('https')591 * , express = require('express')592 * , app = express();593 *594 * http.createServer(app).listen(80);595 * https.createServer({ ... }, app).listen(443);596 *597 * @return {http.Server}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 598why, one step back598 * @publicdeps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 599–600why, one step back599 */600Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 601why, one step back601app.listen = function listen() {deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
lines 602–606why, one step back602 var server = http.createServer(this);603 return server.listen.apply(server, arguments);604};605606/**Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 607–611why, one step back607 * Log error using console.error.608 *609 * @param {Error} err610 * @private611 */deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 612why, one step back612Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 613–614why, one step back613function logerror(err) {614 /* istanbul ignore next */deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 615why, one step back615 if (this.get('env') !== 'test') console.error(err.stack || err.toString());Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 616–628why, one step back616}617618/**619 * Try rendering a view.620 * @private621 */622623function tryRender(view, options, callback) {624 try {625 view.render(options, callback);626 } catch (err) {627 callback(err);628 }deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98
line 629why, one step back629}