app.use()

Mounts middleware functions at an optional path prefix.

Since Express 4 Spec ↗

Syntax

app.use([path,] ...middleware)

Parameters

NameTypeRequiredDescription
path string | RegExp No Optional mount path; defaults to `/` (all requests).
middleware ...function Yes Middleware `(req, res, next)` or error middleware `(err, req, res, next)`, or a Router/sub-app.

Returns

Application — The app instance, for chaining.

Examples

app.use(express.json());
app.use((req, res, next) => {
  req.requestedAt = Date.now();
  next();
});
import usersRouter from './routes/users.js';

app.use('/api/users', usersRouter);
Output
$ curl localhost:3000/api/users
[...]

Notes

Order matters: middleware runs top-to-bottom in the order it is added. A path here matches as a prefix (`/api` also matches `/api/x`), unlike route methods which match exactly. Always call `next()` (or send a response) or the request hangs.

See also