Express router.use() Method

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 3 Code Examples

What you’ll learn

  • How to mount router-level middleware with and without path prefixes.
  • How to mount child routers inside a parent router module.
  • How router-level middleware order affects route handling.
  • How to avoid scope/order mistakes when composing routers.

Syntax

javascript
router.use(middleware)
router.use(path, middleware)
router.use(path, childRouter)
1

Router-level middleware for all routes

javascript
router.use(function (req, res, next) {
  console.log('Router hit:', req.method, req.originalUrl);
  next();
});
2

Path-scoped middleware inside router

javascript
router.use('/admin', function (req, res, next) {
  if (!req.user || !req.user.isAdmin) return res.status(403).send('Forbidden');
  next();
});
3

Mount child router from parent router

javascript
var usersRouter = require('./users-router');
router.use('/users', usersRouter);
// '/users' paths now handled by usersRouter

⚠️ Common pitfalls

  • Mounting middleware after routes that should have used it.
  • Forgetting next() in middleware and causing request hangs.
  • Assuming router-level path prefixes match differently than app-level prefixes.

❓ FAQ

It mounts middleware or another router inside the current router.
Yes. You can use router.use('/admin', middleware) to scope middleware to matching subpaths.
Both mount middleware, but router.use() is scoped to one router module while app.use() is app-level.
Yes. It is common to mount feature routers like router.use('/users', userRouter).
Yes, for matching paths it runs regardless of HTTP method unless filtered later.
Did you know?

router.use() mounts middleware or child routers on a router instance, letting you build modular middleware pipelines.

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.

4 people found this page helpful