Application Object
The app object conventionally denotes the Express application.
Create it by calling the top-level express() function exported by the Express module:
var express = require('express');var app = express();
app.get('/', function (req, res) { res.send('hello world');});
app.listen(3000);import express from 'express';
const app = express();
app.get('/', function (req, res) { res.send('hello world');});
app.listen(3000);import express, { type Express, type Request, type Response } from 'express';
const app: Express = express();
app.get('/', function (req: Request, res: Response) { res.send('hello world');});
app.listen(3000);The app object has methods for
- Routing HTTP requests; see for example, app.METHOD and app.param.
- Configuring middleware; see app.route.
- Rendering HTML views; see app.render.
- Registering a template engine; see app.engine.
It also has settings (properties) that affect how the application behaves; for more information, see Application Settings.
Note
The Express application object can be referred from the request object and the response object as req.app, and res.app, respectively.
Properties
app.locals
The app.locals object has properties that are local variables within the application,
and will be available in templates rendered with res.render.
Warning
The locals object is used by view engines to render a response. The object keys may be
particularly sensitive and should not contain user-controlled input, as it may affect the
operation of the view engine or provide a path to cross-site scripting. Consult the documentation
for the used view engine for additional considerations.
console.dir(app.locals.title);// => 'My App'
console.dir(app.locals.email);// => '[email protected]'Once set, the value of app.locals properties persist throughout the life of the application,
in contrast with res.locals properties that
are valid only for the lifetime of the request.
You can access local variables in templates rendered within the application.
This is useful for providing helper functions to templates, as well as application-level data.
Local variables are available in middleware via req.app.locals (see req.app)
app.locals.title = 'My App';app.locals.strftime = require('strftime');app.mountpath
The app.mountpath property contains one or more path patterns on which a sub-app was mounted.
Note
A sub-app is an instance of express that may be used for handling the request to a route.
var express = require('express');
var app = express(); // the main appvar admin = express(); // the sub app
admin.get('/', function (req, res) { console.log(admin.mountpath); // /admin res.send('Admin Homepage');});
app.use('/admin', admin); // mount the sub appimport express from 'express';
const app = express(); // the main appconst admin = express(); // the sub app
admin.get('/', function (req, res) { console.log(admin.mountpath); // /admin res.send('Admin Homepage');});
app.use('/admin', admin); // mount the sub appimport express, { type Express, type Request, type Response } from 'express';
const app: Express = express(); // the main appconst admin: Express = express(); // the sub app
admin.get('/', function (req: Request, res: Response) { console.log(admin.mountpath); // /admin res.send('Admin Homepage');});
app.use('/admin', admin); // mount the sub appIt is similar to the baseUrl property of the req object, except req.baseUrl
returns the matched URL path, instead of the matched patterns.
If a sub-app is mounted on multiple path patterns, app.mountpath returns the list of
patterns it is mounted on, as shown in the following example.
var admin = express();
admin.get('/', function (req, res) { console.dir(admin.mountpath); // [ '/adm*n', '/manager' ] res.send('Admin Homepage');});
var secret = express();secret.get('/', function (req, res) { console.log(secret.mountpath); // /secr*t res.send('Admin Secret');});
admin.use('/secr*t', secret); // load the 'secret' router on '/secr*t', on the 'admin' sub appapp.use(['/adm*n', '/manager'], admin); // load the 'admin' router on '/adm*n' and '/manager', on the parent appimport express, { type Express, type Request, type Response } from 'express';
const admin: Express = express();
admin.get('/', function (req: Request, res: Response) {