App Configuration & Settings
This page explains the application-level configuration surface behind settings, template-engine registration, and route-parameter preprocessing. The application object is exported from lib/application.js, while createApplication builds the callable app and initializes it before returning it.
These APIs exist so application setup can be expressed declaratively: settings can be written and read, view engines can be associated with extensions, and parameter-specific work can happen before a route handler uses the parameter. The shown tests demonstrate these behaviors through app.enable, app.get, app.engine, app.render, and app.param.
Sources: lib/application.js:40, lib/express.js:36-56, test/app.engine.js:17-30
Core concepts
Application object
The application object is the exported app value that receives configuration and routing methods. createApplication mixes application methods into the callable function, creates request and response prototypes linked back to the app, calls app.init(), and returns it.
Sources: lib/application.js:40, lib/express.js:36-56
Setting
A setting is a named value stored on the application and read back through app.get. The shown configuration test establishes that app.enable('tobi') writes the boolean value true, returns the same app for chaining, and makes that value available through app.get('tobi').
Sources: test/config.js:144-149
Template engine
A template engine is a rendering function registered for a file extension so that app.render can render a matching view. The engine test registers render for .html, configures views, and renders user.html.
Sources: test/app.engine.js:17-30
Route parameter callback
A route parameter callback is a function registered with app.param that receives the request, response, continuation function, and extracted parameter value. In the shown example, it stores the value on req.thing and calls next() before the route handler sends it.
Sources: test/app.param.js:139-142, test/app.param.js:152-158
How settings are written and read
app.enable is the demonstrated convenience operation for setting a named value to true; it returns the application object, which allows setup calls to be chained. The same test also shows that the setting can use the name hasOwnProperty, so the read path must still return the configured value for that name.
describe('.enable()', function(){
it('should set the value to true', function(){
var app = express();
assert.equal(app.enable('tobi'), app);
assert.strictEqual(app.get('tobi'), true);
})
})The important distinction is therefore operational: app.enable performs a write of true, while app.get performs the corresponding read; the test does not show app.set or app.disable implementations, so their detailed behavior is outside the evidence in this pack.
The application module also imports compileETag, compileQueryParser, and compileTrust, indicating that configuration-related compilation helpers are dependencies of the application module, but the supplied lines do not show which setting methods invoke them.
Sources: test/config.js:144-149, test/config.js:151-155, lib/application.js:21, lib/application.js:22, lib/application.js:23
How template engines are wired
app.engine('.html', render) associates the .html extension with the supplied render function. The test then sets views, adds app.locals.user, and calls app.render('user.html',...), which produces the expected HTML string.
The view layer represents engine configuration with a default engine name, an engines require cache, and a root lookup path. When the requested extension is not already present in opts.engines, View derives a module name from the extension, requires that module’s __express export, verifies that the export is a function, stores it in the engine map, and assigns it to this.engine.
This means there are two visible registration paths: an application test explicitly registers render for .html, while View can lazily load a missing extension’s __express export and cache it. The supplied View excerpt also shows that engine selection is followed by view-path lookup through this.lookup(fileName).
The rendering helper tryRender delegates to view.render(options, callback) and converts a synchronous throw into callback(err). The sequence below isolates that error-safe hand-off rather than assuming details of the engine implementation that are not shown.
Evidence
- try-renderlib/application.js:625
- viewlib/application.js:625
- optionslib/application.js:625
- callbacklib/application.js:625
The diagram’s key point is that tryRender does not choose an engine itself in the shown helper; it delegates to the view and guarantees callback-based error reporting for a synchronous exception.
Sources: test/app.engine.js:17-30, lib/view.js:38-52, lib/view.js:75-92, lib/view.js:90-95, lib/application.js:625-631
How route parameters are prepared
app.param('thing',...) registers preprocessing for the thing route parameter. In the shown callback, the extracted thing value is assigned to req.thing, and next() is called so processing can continue.
The GET route /:thing then sends req.thing, and a request for /bob is expected to return bob. This demonstrates the practical effect before the route handler runs: the parameter callback turns the route value into request state that the handler consumes.
The same test registers a separate user parameter callback that raises an error, but attaches it to a POST route while the request shown is a GET request for /:thing. The test name states that parameter processing should not occur without a route handler, so parameter configuration is tied to an actually matched route rather than being invoked merely because it was registered.
Evidence
- route-valuetest/app.param.js:139
- param-callbacktest/app.param.js:139
- request-statetest/app.param.js:139
- route-handlertest/app.param.js:152
The workflow is grounded in the test’s visible data movement: the callback receives the parameter, stores it on the request, and the matching route handler reads that stored value.
Sources: test/app.param.js:139-142, test/app.param.js:152-158, test/app.param.js:136-149, test/app.param.js:139-158
How it connects
Application construction belongs with Application Lifecycle, which describes initialization and server boot; this page only establishes that createApplication calls app.init() before returning the app.
Template-engine registration hands off to Rendering Views, where view lookup and rendering are the larger concern. The supplied View code shows the engine cache, lazy __express loading, selected engine, and lookup path that make that hand-off possible.
Parameter preprocessing feeds App Routing & Request Dispatch, because the shown app.param behavior is attached to a route and affects request state consumed by its handler.
Sources: lib/express.js:36-56, lib/view.js:38-52, lib/view.js:75-95, test/app.param.js:148-158
Key takeaways
- app.enable writes
trueand returns the app; app.get reads the configured value. app.engine('.html', render)registers a renderer for.html, whichapp.render('user.html',...)can use.Viewcaches an engine, can load a module’s__expressexport, and then looks up the view path.- app.param preprocessing can store the extracted value on the request before the matching route handler runs.
tryRenderdelegates rendering and reports synchronous failures through the callback.
Sources: test/config.js:144-149, lib/view.js:75-95, test/app.param.js:139-158, lib/application.js:625-631