app.use([path,] function [, function...])
Mounts the specified middleware function or functions at the specified path. If path
is not specified, it defaults to “/”.
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.
Note that req.originalUrl
in a middleware function is a combination of req.baseUrl
and req.path
, as shown in the following example.
1 2 3 4 5 6 7 | app.use( '/admin' , function (req, res, next) { // GET 'http://www.example.com/admin/new' console.log(req.originalUrl); // '/admin/new' console.log(req.baseUrl); // '/admin' console.log(req.path); // '/new' next(); }); |
Mounting a middleware function at a path
will cause the middleware function to be executed whenever the base of the requested path matches the path
.
Since path
defaults to “/”, middleware mounted without a path will be executed for every request to the app.
1 2 3 4 5 | // this middleware will be executed for every request to the app app.use( function (req, res, next) { console.log( 'Time: %d' , Date.now()); next(); }); |
NOTE
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.
1 2 3 4 5 6 7 8 9 | // 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' ); }); |
path
can be a string representing a path, a path pattern, a regular expression to match paths, or an array of combinations thereof.
The following table provides some simple examples of mounting middleware.
Type | Example | ||||||||
---|---|---|---|---|---|---|---|---|---|
Path | This will match paths starting with `/abcd`:
| ||||||||
Path Pattern | This will match paths starting with `/abcd` and `/abd`:
| ||||||||
Regular Expression | This will match paths starting with `/abc` and `/xyz`:
| ||||||||
Array | This will match paths starting with `/abcd`, `/xyza`, `/lmn`, and `/pqr`:
|
function
can be a middleware function, a series of middleware functions, an array of middleware functions, or a combination of all of them. Since router and app implement the middleware interface, you can use them as you would any other middleware function.
Usage | Example | ||||||
---|---|---|---|---|---|---|---|
Single Middleware | You can define and mount a middleware function locally.
| ||||||
Series of Middleware | You can specify more than one middleware function at the same mount path.
| ||||||
Array | Use an array to group middleware logically. If you pass an array of middleware as the first or only middleware parameters, then you must specify the mount path.
| ||||||
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:
1 2 | // GET /style.css etc app.use(express.static(__dirname + '/public' )); |
Mount the middleware at “/static” to serve static content only when their request path is prefixed with “/static”:
1 2 | // GET /static/style.css etc. app.use( '/static' , express.static(__dirname + '/public' )); |
Disable logging for static content requests by loading the logger middleware after the static middleware:
1 2 | app.use(express.static(__dirname + '/public' )); app.use(logger()); |
Serve static files from multiple directories, but give precedence to “./public” over the others:
1 2 3 | app.use(express.static(__dirname + '/public' )); app.use(express.static(__dirname + '/files' )); app.use(express.static(__dirname + '/uploads' )); |
Please login to continue.