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/5x/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((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((req, res, next) => {
  res.send('Hello World');
});

// requests will never reach this route
app.get('/', (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((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`):
app.use('/abcd', (req, res, next) => {
  next();
});
Path Pattern This will match paths starting with `/abcd` and `/abd`:
app.use('/ab{c}d', (req, res, next) => {
  next();
});
Regular Expression This will match paths starting with `/abc` and `/xyz`:
app.use(/\/abc|\/xyz/, (req, res, next) => {
  next();
});
Array This will match paths starting with `/abcd`, `/xyza`, `/lmn`, and `/pqr`:
app.use(['/abcd', '/xyza', /\/lmn|\/pqr/], (req, res, next) => {
  next();
});

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.
app.use((req, res, next) => {
  next();
});

A router is valid middleware.

const router = express.Router();
router.get('/', (req, res, next) => {
  next();
});
app.use(router);

An Express app is valid middleware.

const subApp = express();
subApp.get('/', (req, res, next) => {
  next();
});
app.use(subApp);
Series of Middleware You can specify more than one middleware function at the same mount path.
const r1 = express.Router();
r1.get('/', (req, res, next) => {
  next();
});

const r2 = express.Router();
r2.get('/', (req, res, next) => {
  next();
});

app.use(r1, r2);
Array Use an array to group middleware logically.
const r1 = express.Router();
r1.get('/', (req, res, next) => {
  next();
});

const r2 = express.Router();
r2.get('/', (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();
}

const r1 = express.Router();
r1.get('/', (req, res, next) => {
  next();
});

const r2 = express.Router();
r2.get('/', (req, res, next) => {
  next();
});

const subApp = express();
subApp.get('/', (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(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')));