Skip to content
Myr

application.js

This file

Path

lib/application.js

631 lines · 13.6 KiB · JavaScript

Last changed by

refactor: use cached slice in app.listen (#6897)TheMysteriousbefore adoption54af593

History of this file

Take it

The walk stopped after 200 commits: lines older than 7f25328 point at it.

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 1–8why, one step back
1/*!
2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2013 Roman Shtylman5 * Copyright(c) 2014-2015 Douglas Christopher Wilson6 * MIT Licensed7 */8

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 9–10why, one step back
9'use strict';
10

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 11–12why, one step back
11/**
12 * Module dependencies.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 13why, one step back
13 * @private

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 14–18why, one step back
14 */
1516var finalhandler = require('finalhandler');17var debug = require('debug')('express:application');18var View = require('./view');

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

line 19why, one step back
19var http = require('node:http');

chore: replace `methods` dependency with standard library (#6196)Jon Koopsbefore adoptionb11122b

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 21–23why, one step back
21var compileETag = require('./utils').compileETag;
22var compileQueryParser = require('./utils').compileQueryParser;23var compileTrust = require('./utils').compileTrust;

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

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

call callback once on listen errorWesbefore adoptionee40a88

line 25why, one step back
25var once = require('once')

Use router module for routingDouglas Christopher Wilsonbefore adoptioncec5780

line 26why, one step back
26var Router = require('router');

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 27–32why, one step back
27
28/**29 * Module variables.30 * @private31 */32

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

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

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

line 34why, one step back
34var flatten = Array.prototype.flat;

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 35–42why, one step back
35
36/**37 * Application prototype.38 */3940var app = exports = module.exports = {};4142/**

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 44why, one step back
44 * @private

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 45–49why, one step back
45 */
4647var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';4849/**

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 50–55why, one step back
50 * Initialize the server.
51 *52 * - setup default configuration53 * - setup default middleware54 * - setup route reflection methods55 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 56why, one step back
56 * @private

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 57–58why, one step back
57 */
58

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 60–61why, one step back
60 var router = null;
61

Use object with null prototype for various app propertiesEvan Hahnbefore adoption1443973

lines 62–64why, one step back
62 this.cache = Object.create(null);
63 this.engines = Object.create(null);64 this.settings = Object.create(null);

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 65why, one step back
65

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 66–86why, one step back
66 this.defaultConfiguration();
6768 // Setup getting to lazily add base router69 Object.defineProperty(this, 'router', {70 configurable: true,71 enumerable: true,72 get: function getrouter() {73 if (router === null) {74 router = new Router({75 caseSensitive: this.enabled('case sensitive routing'),76 strict: this.enabled('strict routing')77 });78 }7980 return router;81 }82 });83};8485/**86 * Initialize application configuration.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 87why, one step back
87 * @private

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 88–89why, one step back
88 */
89

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 90–92why, one step back
90app.defaultConfiguration = function defaultConfiguration() {
91 var env = process.env.NODE_ENV || 'development';92

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 93–96why, one step back
93 // default settings
94 this.enable('x-powered-by');95 this.set('etag', 'weak');96 this.set('env', env);

Change query parser setting default to 'simple'Czarekbefore adoption450c468

line 97why, one step back
97 this.set('query parser', 'simple')

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 98–100why, one step back
98 this.set('subdomain offset', 2);
99 this.set('trust proxy', false);100

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 101–106why, one step back
101 // trust proxy inherit back-compat
102 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {103 configurable: true,104 value: true105 });106

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 109–117why, one step back
109 this.on('mount', function onmount(parent) {
110 // inherit trust proxy111 if (this.settings[trustProxyDefaultSymbol] === true112 && typeof parent.settings['trust proxy fn'] === 'function') {113 delete this.settings['trust proxy'];114 delete this.settings['trust proxy fn'];115 }116117 // inherit protos

refactor: Remove Object.setPrototypeOf polyfill (#6081)Phillip Bartabefore adoption8cb53ea

lines 118–121why, one step back
118 Object.setPrototypeOf(this.request, parent.request)
119 Object.setPrototypeOf(this.response, parent.response)120 Object.setPrototypeOf(this.engines, parent.engines)121 Object.setPrototypeOf(this.settings, parent.settings)

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 122–145why, one step back
122 });
123124 // setup locals125 this.locals = Object.create(null);126127 // top-most app is mounted at /128 this.mountpath = '/';129130 // default locals131 this.locals.settings = this.settings;132133 // default configuration134 this.set('view', View);135 this.set('views', resolve('views'));136 this.set('jsonp callback name', 'callback');137138 if (env === 'production') {139 this.enable('view cache');140 }141};142143/**144 * Dispatch a req, res pair into the application. Starts pipeline processing.145 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 149why, one step back
149 * @private

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 150–151why, one step back
150 */
151

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 155–168why, one step back
155 env: this.get('env'),
156 onerror: logerror.bind(this)157 });158159 // set powered by header160 if (this.enabled('x-powered-by')) {161 res.setHeader('X-Powered-By', 'Express');162 }163164 // set circular references165 req.res = res;166 res.req = req;167168 // alter the prototypes

refactor: Remove Object.setPrototypeOf polyfill (#6081)Phillip Bartabefore adoption8cb53ea

lines 169–170why, one step back
169 Object.setPrototypeOf(req, this.request)
170 Object.setPrototypeOf(res, this.response)

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 171–186why, one step back
171
172 // setup locals173 if (!res.locals) {174 res.locals = Object.create(null);175 }176177 this.router.handle(req, res, done);178};179180/**181 * Proxy `Router#use()` to add middleware to the app router.182 * See Router#use() documentation for details.183 *184 * If the _fn_ parameter is an express app, then it will be185 * mounted at the _route_ specified.186 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 187why, one step back
187 * @public

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 188–209why, one step back
188 */
189190app.use = function use(fn) {191 var offset = 0;192 var path = '/';193194 // default path to '/'195 // disambiguate app.use([fn])196 if (typeof fn !== 'function') {197 var arg = fn;198199 while (Array.isArray(arg) && arg.length !== 0) {200 arg = arg[0];201 }202203 // first arg is the path204 if (typeof arg !== 'function') {205 offset = 1;206 path = fn;207 }208 }209

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

line 210why, one step back
210 var fns = flatten.call(slice.call(arguments, offset), Infinity);

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 211–212why, one step back
211
212 if (fns.length === 0) {

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 213why, one step back
213 throw new TypeError('app.use() requires a middleware function')

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 214–232why, one step back
214 }
215216 // get router217 var router = this.router;218219 fns.forEach(function (fn) {220 // non-express app221 if (!fn || !fn.handle || !fn.set) {222 return router.use(path, fn);223 }224225 debug('.use app under %s', path);226 fn.mountpath = path;227 fn.parent = this;228229 // restore .app property on req and res230 router.use(path, function mounted_app(req, res, next) {231 var orig = req.app;232 fn.handle(req, res, function (err) {

refactor: Remove Object.setPrototypeOf polyfill (#6081)Phillip Bartabefore adoption8cb53ea

lines 233–234why, one step back
233 Object.setPrototypeOf(req, orig.request)
234 Object.setPrototypeOf(res, orig.response)

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 235–252why, one step back
235 next(err);
236 });237 });238239 // mounted an app240 fn.emit('mount', this);241 }, this);242243 return this;244};245246/**247 * Proxy to the app `Router#route()`248 * Returns a new `Route` instance for the _path_.249 *250 * Routes are isolated middleware stacks for specific paths.251 * See the Route api docs for details.252 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 253why, one step back
253 * @public

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 254–265why, one step back
254 */
255256app.route = function route(path) {257 return this.router.route(path);258};259260/**261 * Register the given template engine callback `fn`262 * as `ext`.263 *264 * By default will `require()` the engine based on the265 * file extension. For example if you try to render

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 266why, one step back
266 * a "foo.ejs" file Express will invoke the following internally:

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

line 267why, one step back
267 *

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 268why, one step back
268 * app.engine('ejs', require('ejs').__express);

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 269–279why, one step back
269 *
270 * For engines that do not provide `.__express` out of the box,271 * or if you wish to "map" a different extension to the template engine272 * you may use this method. For example mapping the EJS template engine to273 * ".html" files:274 *275 * app.engine('html', require('ejs').renderFile);276 *277 * In this case EJS provides a `.renderFile()` method with278 * the same signature that Express expects: `(path, options, callback)`,279 * though note that it aliases this method as `ejs.__express` internally

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 280why, one step back
280 * so if you're using ".ejs" extensions you don't need to do anything.

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 281–290why, one step back
281 *
282 * Some template engines do not follow this convention, the283 * [Consolidate.js](https://github.com/tj/consolidate.js)284 * library was created to map all of node's popular template285 * engines to follow this convention, thus allowing them to286 * work seamlessly within Express.287 *288 * @param {String} ext289 * @param {Function} fn290 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 291why, one step back
291 * @public

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 292–293why, one step back
292 */
293

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 294–306why, one step back
294app.engine = function engine(ext, fn) {
295 if (typeof fn !== 'function') {296 throw new Error('callback function required');297 }298299 // get file extension300 var extension = ext[0] !== '.'301 ? '.' + ext302 : ext;303304 // store engine305 this.engines[extension] = fn;306

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 307–318why, one step back
307 return this;
308};309310/**311 * Proxy to `Router#param()` with one added api feature. The _name_ parameter312 * can be an array of names.313 *314 * See the Router#param() docs for more details.315 *316 * @param {String|Array} name317 * @param {Function} fn318 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 319why, one step back
319 * @public

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 320–326why, one step back
320 */
321322app.param = function param(name, fn) {323 if (Array.isArray(name)) {324 for (var i = 0; i < name.length; i++) {325 this.param(name[i], fn);326 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 327why, one step back
327

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 328–331why, one step back
328 return this;
329 }330331 this.router.param(name, fn);

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 332why, one step back
332

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 333–339why, one step back
333 return this;
334};335336/**337 * Assign `setting` to `val`, or return `setting`'s value.338 *339 * app.set('foo', 'bar');

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 340why, one step back
340 * app.set('foo');

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 341–347why, one step back
341 * // => "bar"
342 *343 * Mounted servers inherit their parent server's settings.344 *345 * @param {String} setting346 * @param {*} [val]347 * @return {Server} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 348why, one step back
348 * @public

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 349–350why, one step back
349 */
350

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 352–353why, one step back
352 if (arguments.length === 1) {
353 // app.get(setting)

Use object with null prototype for settings closes #4835Evan Hahnbefore adoption26801a0

line 354why, one step back
354 return this.settings[setting];

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 355–356why, one step back
355 }
356

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 359–371why, one step back
359 // set value
360 this.settings[setting] = val;361362 // trigger matched settings363 switch (setting) {364 case 'etag':365 this.set('etag fn', compileETag(val));366 break;367 case 'query parser':368 this.set('query parser fn', compileQueryParser(val));369 break;370 case 'trust proxy':371 this.set('trust proxy fn', compileTrust(val));

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 372–378why, one step back
372
373 // trust proxy inherit back-compat374 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {375 configurable: true,376 value: false377 });378

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 379–395why, one step back
379 break;
380 }381382 return this;383};384385/**386 * Return the app's absolute pathname387 * based on the parent(s) that have388 * mounted it.389 *390 * For example if the application was391 * mounted as "/admin", which itself392 * was mounted as "/blog" then the393 * return value would be "/blog/admin".394 *395 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 396why, one step back
396 * @private

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 397–398why, one step back
397 */
398

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 400–416why, one step back
400 return this.parent
401 ? this.parent.path() + this.mountpath402 : '';403};404405/**406 * Check if `setting` is enabled (truthy).407 *408 * app.enabled('foo')409 * // => false410 *411 * app.enable('foo')412 * app.enabled('foo')413 * // => true414 *415 * @param {String} setting416 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 417why, one step back
417 * @public

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 418–419why, one step back
418 */
419

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 422–435why, one step back
422};
423424/**425 * Check if `setting` is disabled.426 *427 * app.disabled('foo')428 * // => true429 *430 * app.enable('foo')431 * app.disabled('foo')432 * // => false433 *434 * @param {String} setting435 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 436why, one step back
436 * @public

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 437–438why, one step back
437 */
438

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 440–447why, one step back
440 return !this.set(setting);
441};442443/**444 * Enable `setting`.445 *446 * @param {String} setting447 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 448why, one step back
448 * @public

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 449–450why, one step back
449 */
450

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 452–459why, one step back
452 return this.set(setting, true);
453};454455/**456 * Disable `setting`.457 *458 * @param {String} setting459 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 460why, one step back
460 * @public

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 461–462why, one step back
461 */
462

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 464–470why, one step back
464 return this.set(setting, false);
465};466467/**468 * Delegate `.VERB(...)` calls to `router.VERB(...)`.469 */470

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

lines 471–472why, one step back
471methods.forEach(function (method) {
472 app[method] = function (path) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 473–476why, one step back
473 if (method === 'get' && arguments.length === 1) {
474 // app.get(setting)475 return this.set(path);476 }

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 477–490why, one step back
477
478 var route = this.route(path);479 route[method].apply(route, slice.call(arguments, 1));480 return this;481 };482});483484/**485 * Special-cased "all" method, applying the given route `path`,486 * middleware, and callback to _every_ HTTP method.487 *488 * @param {String} path489 * @param {Function} ...490 * @return {app} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 491why, one step back
491 * @public

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 492–493why, one step back
492 */
493

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 495–497why, one step back
495 var route = this.route(path);
496 var args = slice.call(arguments, 1);497

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 498–501why, one step back
498 for (var i = 0; i < methods.length; i++) {
499 route[methods[i]].apply(route, args);500 }501

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 502–516why, one step back
502 return this;
503};504505/**506 * Render the given view `name` name with `options`507 * and a callback accepting an error and the508 * rendered template string.509 *510 * Example:511 *512 * app.render('email', { name: 'Tobi' }, function(err, html){513 * // ...514 * })515 *516 * @param {String} name

Merge tag '4.13.4'Douglas Christopher Wilsonbefore adoption7cafdb5

line 517why, one step back
517 * @param {Object|Function} options or fn

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 518–519why, one step back
518 * @param {Function} callback
519 * @public

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 520–521why, one step back
520 */
521

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 526why, one step back
526 var opts = options;

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 527–529why, one step back
527 var view;
528529 // support callback function as second arg

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 530–532why, one step back
530 if (typeof options === 'function') {
531 done = options;532 opts = {};

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 533–535why, one step back
533 }
534535 // merge options

fix: Remove `utils-merge` dependency - use spread syntax instead (#6091)Phillip Bartabefore adoption246f6f5

line 536why, one step back
536 var renderOptions = { ...this.locals, ...opts._locals, ...opts };

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 537–538why, one step back
537
538 // set .cache unless explicitly provided

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 539–541why, one step back
539 if (renderOptions.cache == null) {
540 renderOptions.cache = this.enabled('view cache');541 }

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 542–543why, one step back
542
543 // primed cache

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 544–546why, one step back
544 if (renderOptions.cache) {
545 view = cache[name];546 }

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 547–549why, one step back
547
548 // view549 if (!view) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 550–552why, one step back
550 var View = this.get('view');
551552 view = new View(name, {

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 553–563why, one step back
553 defaultEngine: this.get('view engine'),
554 root: this.get('views'),555 engines: engines556 });557558 if (!view.path) {559 var dirs = Array.isArray(view.root) && view.root.length > 1560 ? 'directories "' + view.root.slice(0, -1).join('", "') + '" or "' + view.root[view.root.length - 1] + '"'561 : 'directory "' + view.root + '"'562 var err = new Error('Failed to lookup view "' + name + '" in views ' + dirs);563 err.view = view;

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 565–567why, one step back
565 }
566567 // prime the cache

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 568–570why, one step back
568 if (renderOptions.cache) {
569 cache[name] = view;570 }

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 571–573why, one step back
571 }
572573 // render

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 575–585why, one step back
575};
576577/**578 * Listen for connections.579 *580 * A node `http.Server` is returned, with this581 * application (which is a `Function`) as its582 * callback. If you wish to create both an HTTP583 * and HTTPS server you may do so with the "http"584 * and "https" modules as shown here:585 *

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

lines 586–587why, one step back
586 * var http = require('node:http')
587 * , https = require('node:https')

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 588–594why, one step back
588 * , express = require('express')
589 * , app = express();590 *591 * http.createServer(app).listen(80);592 * https.createServer({ ... }, app).listen(443);593 *594 * @return {http.Server}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 595why, one step back
595 * @public

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 596–597why, one step back
596 */
597

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

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

call callback once on listen errorWesbefore adoptionee40a88

line 599why, one step back
599 var server = http.createServer(this)

refactor: use cached slice in app.listen (#6897)TheMysteriousbefore adoption54af593

line 600why, one step back
600 var args = slice.call(arguments)

call callback once on listen errorWesbefore adoptionee40a88

lines 601–606why, one step back
601 if (typeof args[args.length - 1] === 'function') {
602 var done = args[args.length - 1] = once(args[args.length - 1])603 server.once('error', done)604 }605 return server.listen.apply(server, args)606}

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

lines 607–608why, one step back
607
608/**

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 609–613why, one step back
609 * Log error using console.error.
610 *611 * @param {Error} err612 * @private613 */

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

line 614why, one step back
614

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 618–630why, one step back
618}
619620/**621 * Try rendering a view.622 * @private623 */624625function tryRender(view, options, callback) {626 try {627 view.render(options, callback);628 } catch (err) {629 callback(err);630 }

Remove req.acceptsCharsetDouglas Christopher Wilsonbefore adoption7f25328

line 631why, one step back
631}