Express.js router.use() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Router

Definition

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.

Express
router.use([path], callback [, callback...])
router.use([path], router)
CallWhat it does
router.use([path], callback [, callback...])Primary signature.
router.use([path], router)Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

router.use([path], callback [, callback...])
router.use([path], router)

Parameters

ParameterDescription
pathOptional. Optional mount path (default /). Prefix match relative to this router’s mount. May include named params such as /users/:userId.
callbackRequired. One or more middleware functions (req, res, next), or another router to nest.

Return Value

router.use() returns the same Router so you can chain more .use() / route calls if you want. Most code ignores the return value.

Express
router.use(requireAuth)
  .use('/files', filesRouter);

// typical style:
router.use(requireAuth);
router.use('/files', filesRouter);

Express.js Examples

Example 1 — Logger + Path Middleware

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);

How It Works

  1. App mount. app.use('/foo', router) sends /foo… into this module.
  2. First use. No path means every request that entered the router is logged.
  3. Prefix /bar. Runs for /bar and /bar/1. Inside, the mount is stripped — req.path is / or /1.
  4. 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.

Express
const express = require('express');
const app = express();

const api = express.Router();
const users = express.Router();

api.use((req, res, next) => {
  console.log('api', req.method, req.path);
  next();
});

users.get('/', (req, res) => {
  res.send('users-list');
});

users.get('/:id', (req, res) => {
  res.json({ id: req.params.id, base: req.baseUrl });
});

api.use('/users', users);
api.get('/health', (req, res) => {
  res.send('ok');
});

app.use('/api', api);
app.listen(3000);

How It Works

  1. Parent pipe. api.use logs every request under /api.
  2. Nest. api.use('/users', users) mounts the child. Public list URL is /api/users.
  3. Relative routes. Inside users, get('/') and get('/:id') omit the /api/users prefix.
  4. req.baseUrl. Shows the mounts stacked so far (/api/users).

Example 3 — Named Mount Params

Docs tip: mount paths can include named parameters so parent router.param can preload values for nested routers.

Express
const express = require('express');
const app = express();

const parent = express.Router({ mergeParams: true });
const posts = express.Router({ mergeParams: true });

parent.param('userId', (req, res, next, id) => {
  req.user = { id };
  next();
});

posts.get('/', (req, res) => {
  res.json({ userId: req.params.userId, user: req.user });
});

parent.use('/users/:userId/posts', posts);
app.use(parent);
app.listen(3000);

How It Works

  1. Named mount. parent.use('/users/:userId/posts', posts) captures userId while mounting.
  2. Param preload. parent.param('userId') runs before the child route.
  3. mergeParams: true. Lets the child see parent params on req.params.
  4. Child path. posts.get('/') answers the list under that mount.

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.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via 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.

Next: app.use()

Mount middleware and routers on the application itself.

app.use() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful