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.
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.
// 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.
// 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`: app.use('/abcd', function (req, res, next) { next(); }); |
Path Pattern | This will match paths starting with `/abcd` and `/abd`: app.use('/abc?d', function (req, res, next) { next(); });This will match paths starting with `/abcd`, `/abbcd`, `/abbbbbcd`, and so on: app.use('/ab+cd', function (req, res, next) { next(); });This will match paths starting with `/abcd`, `/abxcd`, `/abFOOcd`, `/abbArcd`, and so on: app.use('/ab\*cd', function (req, res, next) { next(); });This will match paths starting with `/ad` and `/abcd`: app.use('/a(bc)?d', function (req, res, next) { next(); }); |
Regular Expression | This will match paths starting with `/abc` and `/xyz`: app.use(/\/abc|\/xyz/, function (req, res, next) { next(); }); |
Array | This will match paths starting with `/abcd`, `/xyza`, `/lmn`, and `/pqr`: app.use(['/abcd', '/xyza', /\/lmn|\/pqr/], function (req, res, next) { next(); }); |
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. app.use(function (req, res, next) { next(); });A router is valid middleware. var router = express.Router(); router.get('/', function (req, res, next) { next(); }); app.use(router);An Express app is valid middleware. var subApp = express(); subApp.get('/', function (req, res, next) { next(); }); app.use(subApp); |
Series of Middleware | You can specify more than one middleware function at the same mount path. var r1 = express.Router(); r1.get('/', function (req, res, next) { next(); }); var r2 = express.Router(); r2.get('/', function (req, res, next) { next(); }); app.use(r1, r2); |
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. var r1 = express.Router(); r1.get('/', function (req, res, next) { next(); }); var r2 = express.Router(); r2.get('/', function (req, res, next) { next(); }); app.use('/', [r1, r2]); |
Combination | You can combine all the above ways of mounting middleware. function mw1(req, res, next) { next(); } function mw2(req, res, next) { next(); } var r1 = express.Router(); r1.get('/', function (req, res, next) { next(); }); var r2 = express.Router(); r2.get('/', function (req, res, next) { next(); }); var subApp = express(); subApp.get('/', function (req, res, next) { next(); }); app.use(mw1, [mw2, r1, r2], subApp); |
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(__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(__dirname + '/public'));
Disable logging for static content requests by loading the logger middleware after the static middleware:
app.use(express.static(__dirname + '/public')); app.use(logger());
Serve static files from multiple directories, but give precedence to “./public” over the others:
app.use(express.static(__dirname + '/public')); app.use(express.static(__dirname + '/files')); app.use(express.static(__dirname + '/uploads'));
Please login to continue.