Overview
Theapp object is the Express application. It’s created by calling express() and represents the top-level of your application.
Properties
app.locals
Local variables scoped to the application. These variables are available within the application and are useful for providing helper functions or application-level data to templates.app.locals properties persist throughout the life of the application, unlike res.locals which are scoped to the request.app.mountpath
Contains the path pattern(s) on which a sub-app was mounted.Settings Methods
app.set(setting, value)
Assignsvalue to the application setting setting. When called with only one argument, returns the setting’s value.
The name of the setting
The value to assign to the setting
- View Settings
- Network Settings
- Response Settings
- Other Settings
views- Directory for view templates (default:process.cwd() + '/views')view engine- Default template engine extensionview cache- Enables view template compilation caching (enabled in production)
app.get(setting)
Returns the value of the application settingsetting.
The name of the setting to retrieve
app.enable(setting)
Sets the boolean settingsetting to true.
The name of the setting to enable
app.disable(setting)
Sets the boolean settingsetting to false.
The name of the setting to disable
app.enabled(setting)
Returnstrue if the setting setting is enabled (truthy).
The name of the setting to check
app.disabled(setting)
Returnstrue if the setting setting is disabled (falsy).
The name of the setting to check
Middleware & Routing Methods
app.use([path], …callbacks)
Mounts middleware function(s) at the specified path. The middleware function is executed when the base of the requested path matchespath.
The path for which the middleware is invoked
Middleware function(s) or an array of middleware functions
app.route(path)
Returns an instance of a single route which you can then use to handle HTTP verbs with optional middleware. Useapp.route() to avoid duplicate route naming and thus typing errors.
The path for the route
HTTP Method Routes
Express supports the following routing methods corresponding to HTTP methods:app.get(path, ...callbacks)app.post(path, ...callbacks)app.put(path, ...callbacks)app.delete(path, ...callbacks)app.patch(path, ...callbacks)app.head(path, ...callbacks)app.options(path, ...callbacks)
The path for which the handler is invoked
Callback function(s) to handle the request
- GET
- POST
- PUT
- DELETE
app.all(path, …callbacks)
Matches all HTTP methods on the specified path. Useful for defining middleware that applies to all HTTP verbs.The path for which the handler is invoked
Callback function(s) to handle the request
app.param(name, callback)
Adds callback triggers to route parameters. Thename parameter can be a single parameter name or an array of parameter names.
The name(s) of the route parameter(s)
The callback function with signature
(req, res, next, value, name)View Rendering
app.render(view, [options], callback)
Renders aview with a callback. Accepts an optional options object. This is used internally by res.render().
The name of the view file to render
An object whose properties define local variables for the view
Callback function with signature
(err, html)app.engine(ext, callback)
Registers the given template enginecallback as ext.
The file extension (with or without leading dot)
The template engine function with signature
(path, options, callback)Express will
require() the engine internally if it provides a .__express method. For engines without this method, use this function to map extensions.Server Methods
app.listen([port], [hostname], [backlog], [callback])
Starts the application and binds it to a port. This method is identical to Node.js’shttp.Server.listen().
The port number to listen on (default: 3000)
The hostname to bind to
The maximum length of the queue of pending connections
Callback function called once the server starts listening
app.listen() method returns an http.Server object:
If you want to create both HTTP and HTTPS servers, use the Node.js
http and https modules directly:Events
mount
Themount event is fired on a sub-app when it’s mounted on a parent app. The parent app is passed to the callback function.