Express req.baseUrl Property

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 4 Code Examples

What you’ll learn

  • What req.baseUrl contains during request handling.
  • How mount paths work with routers and sub-apps.
  • How req.baseUrl differs from req.path and req.originalUrl.
  • How to use it for logging and link generation.

Usage syntax

javascript
req.baseUrl
1

Mounted router base path

javascript
var usersRouter = express.Router();

usersRouter.get('/:id', function (req, res) {
  res.json({
    baseUrl: req.baseUrl,
    path: req.path,
    originalUrl: req.originalUrl
  });
});

app.use('/users', usersRouter);
2

Nested routers and base URL

javascript
var apiRouter = express.Router();
var v1Router = express.Router();

v1Router.get('/status', function (req, res) {
  res.send('Mounted at: ' + req.baseUrl); // /api/v1
});

apiRouter.use('/v1', v1Router);
app.use('/api', apiRouter);

❓ FAQ

It is the URL path on which the current router or middleware was mounted.
req.baseUrl is the mount point, while req.path is the remaining path within the current route context.
Yes. On top-level app routes that are not mounted under a prefix, it may be an empty string.
No. It represents only the matched mount path segment.
Yes. It helps build logs, links, and diagnostics that respect router mount structure.
Did you know?

req.baseUrl shows the matched mount path for the current router, not the full request URL.

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