In Express.js, app.use([path], callback)mounts middleware (or a Router) into the app. The function runs when the request path matches — by default for every request if you omit path.
Official rule: with a string path, app.use matches that path and its subpaths (/apple matches /apple/images). app.get('/apple') matches only the whole path /apple. Always call next() unless you end the response.
Syntax
Call app.use in your Express.js app. Start with this line.
GET /users
→ 200 [{ "name": "Ada" }]
POST /users
{"name":"Lin"}
→ 201 { "name": "Lin" }
How It Works
Parse JSON.app.use(express.json()) fills req.body for POSTs.
Define routes. Paths on the router are relative (/).
Mount.app.use('/users', users) → full URLs /users, …
Same API. Routers are middleware — that is why use accepts them.
Compatibility
Runtime Support
app.use() is an Express.js application method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only send requests into your middleware stack.
✓ Server · Express 4 & 5
app.use()
Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers.
ServerNode.js runtime API
Node.js18+ for Express 5 · 14+ typical for Express 4
Supported
BunExpress-compatible server apps
Supported
DenoVia npm compatibility / adapters
Supported
app.use()Express on Node.js
Bottom line: Register middleware with app.use, remember next(), then start the server.
Frequently Asked Questions
It mounts middleware (or a Router) into the request pipeline. With a path, it runs when the request path starts with that prefix; without a path, it runs for every request.
app.get matches one verb and the whole path. app.use matches every HTTP method and, for string paths, the path plus subpaths (prefix match).
If it does not end the response, it must call next() so the next middleware or route can run. Otherwise the request hangs.
Yes. app.use('/api', router) is the standard way to attach modular routes under a prefix.
Use four arguments (err, req, res, next). Express only treats it as error middleware when all four are declared — official Caution.