app.use([path,] callback [, callback...])
Mounts the specified middleware function or functions
at the specified path:
the middleware function is executed when the base of the requested path matches path.
{% include api/en/4x/routing-args.html %}
Description
A route will match any path that follows its path immediately with a “/”.
For example: app.use('/apple', ...) will match “/apple”, “/apple/images”,
“/apple/images/news”, and so on.
Since path defaults to ”/”, middleware mounted without a path will be executed for every request to the app.
For example, this middleware function will be executed for every request to the app:
app.use(function (req, res, next) {
console.log('Time: %d', Date.now());
next();
});
Sub-apps will:
- Not inherit the value of settings that have a default value. You must set the value in the sub-app.
- Inherit the value of settings with no default value.
For details, see Application settings.
Middleware functions are executed sequentially, therefore the order of middleware inclusion is important.
// this middleware will not allow the request to go beyond it
app.use(function (req, res, next) {
res.send('Hello World');
});
// requests will never reach this route
app.get('/', function (req, res) {
res.send('Welcome');
});
Error-handling middleware
Error-handling middleware always takes four arguments. You must provide four arguments to identify it as an error-handling middleware function. Even if you don’t need to use the next object, you must specify it to maintain the signature. Otherwise, the next object will be interpreted as regular middleware and will fail to handle errors. For details about error-handling middleware, see: [Error handling](/{{ page.lang }}/guide/error-handling.html).
Define error-handling middleware functions in the same way as other middleware functions, except with four arguments instead of three, specifically with the signature (err, req, res, next)):
app.use(function (err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
Path examples
The following table provides some simple examples of valid path values for
mounting middleware.
| Type | Example |
|---|---|
| Path |
Matches the exact path `/abcd` and any sub-paths starting with `/abcd/` (for example, `/abcd/foo`):
|
| Path Pattern |
This will match paths starting with `/abcd` and `/abd`:
This will match paths starting with
This will match paths starting with
This will match paths starting with
|
| Regular Expression |
This will match paths starting with `/abc` and `/xyz`:
|
| Array |
This will match paths starting with `/abcd`, `/xyza`, `/lmn`, and `/pqr`:
|
Middleware callback function examples
The following table provides some simple examples of middleware functions that
can be used as the callback argument to app.use(), app.METHOD(), and app.all().
| Usage | Example |
|---|---|
| Single Middleware |
You can define and mount a middleware function locally.
A router is valid middleware.
An Express app is valid middleware.
|
| Series of Middleware |
You can specify more than one middleware function at the same mount path.
|
| Array |
Use an array to group middleware logically.
|
| Combination |
You can combine all the above ways of mounting middleware.
|
Following are some examples of using the express.static middleware in an Express app.
Serve static content for the app from the “public” directory in the application directory:
// GET /style.css etc
app.use(express.static(path.join(__dirname, 'public')));
Mount the middleware at “/static” to serve static content only when their request path is prefixed with “/static”:
// GET /static/style.css etc.
app.use('/static', express.static(path.join(__dirname, 'public')));
Disable logging for static content requests by loading the logger middleware after the static middleware:
app.use(express.static(path.join(__dirname, 'public')));
app.use(logger());
Serve static files from multiple directories, but give precedence to ”./public” over the others:
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'files')));
app.use(express.static(path.join(__dirname, 'uploads')));