In Express.js, router.use([path], …fn) mounts middleware — or another express.Router() — on the current router. The optional path defaults to /. Matching requests run those functions in the order you define them.
Official docs: this method is similar to app.use(). The mount path is stripped, so child handlers see only the remaining path. After app.use('/api', router), a router.use('/users', usersRouter) serves public URLs under /api/users.
Syntax
Call router.use in your Express.js app. Start with this line.
Official-style pipe: log every request that enters the router, then run extra logic only under /bar.
Express
const express = require('express');
const app = express();
const router = express.Router();
// every request that enters this router
router.use((req, res, next) => {
console.log('%s %s %s', req.method, req.url, req.path);
next();
});
// only when the remaining path starts with /bar
router.use('/bar', (req, res, next) => {
console.log('bar middleware, path=', req.path);
next();
});
router.use((req, res) => {
res.send('Hello World');
});
app.use('/foo', router);
app.listen(3000);
Output
GET /foo
→ log: GET / / then "Hello World"
GET /foo/bar
→ log: GET /bar /bar
→ bar middleware, path= /
→ "Hello World"
GET /foo/bar/1
→ log + bar middleware (path=/1)
→ "Hello World"
How It Works
App mount.app.use('/foo', router) sends /foo… into this module.
First use. No path means every request that entered the router is logged.
Prefix /bar. Runs for /bar and /bar/1. Inside, the mount is stripped — req.path is / or /1.
Final handler. Sends the body after earlier middleware called next().
Example 2 — Nested Routers
Compose an API module: shared logger, then mount a users router under /users.
mergeParams: true. Lets the child see parent params on req.params.
Child path.posts.get('/') answers the list under that mount.
Compatibility
Runtime Support
router.use() is an Express.js router method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only send HTTP requests.
✓ Server · Express 4 & 5
router.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
router.use()Express on Node.js
Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.
Frequently Asked Questions
It mounts middleware or another router on the current router, with an optional path that defaults to /. Matching requests run those functions in definition order.
router.use() uses prefix matching and strips the mount path from req.url/req.path for child handlers. router.all() is a route that matches one full remaining path for every HTTP method.
Same idea: mount middleware with an optional path. router.use() lives on an express.Router(), so paths are relative to where you mount that router with app.use().
No. Express strips the matched mount. Under router.use('/files', fn) for /files/report, the middleware sees path /report.
No. It is a server-side Express routing helper on Node.js.