Express.js req.baseUrl Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

In Express.js, req.baseUrl is the URL path on which the current router was mounted. If you mount with app.use('/greet', greetRouter), then inside that router req.baseUrl is /greet.

It is similar to app.mountpath, except mountpath can return the pattern(s) you registered, while req.baseUrl always returns the matched string for this request. It is a server-side property on the request object.

Syntax

Read req.baseUrl in your Express.js route handler. Start with this line.

Express
req.baseUrl
CallWhat it does
req.baseUrlPrimary signature.

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

req.baseUrl

Parameters

ParameterDescription
Mounted routerRequired. Without a mount, baseUrl is usually ''
Mount path in app.useRequired. Becomes the value of req.baseUrl when matched
Route path on the routerRequired. Shows up in req.path, not in baseUrl

Return Value

req.baseUrl is a string property: the URL path on which a router was mounted. On top-level app routes it is usually an empty string.

Express
// app.use('/api', apiRouter)
// GET /api/users → inside apiRouter:
req.baseUrl; // '/api'
req.path;    // '/users'

Express.js Examples

Example 1 — Mounted Router (Official Pattern)

Mount a greet router at /greet. Inside the route, req.baseUrl is the mount path.

Express
const express = require('express');
const app = express();
const greet = express.Router();

greet.get('/jp', (req, res) => {
  console.log(req.baseUrl); // /greet
  res.send('Konnichiwa!');
});

app.use('/greet', greet);

app.listen(3000);

How It Works

  1. Define routes on the router. greet.get('/jp', …) is relative to the mount, not the full URL.
  2. Mount with a prefix. app.use('/greet', greet) makes the public URL /greet/jp.
  3. Read the mount path. Inside the handler, req.baseUrl is /greet.
  4. Respond as usual. The client still sees the full path /greet/jp.

Example 2 — Compare baseUrl, path, and originalUrl

Log all three on one request so you can see how Express splits a mounted URL.

Express
const express = require('express');
const app = express();
const admin = express.Router();

admin.get('/new', (req, res) => {
  res.json({
    baseUrl: req.baseUrl,         // '/admin'
    path: req.path,               // '/new'
    originalUrl: req.originalUrl  // '/admin/new?sort=desc'
  });
});

app.use('/admin', admin);

app.listen(3000);

How It Works

  1. Mount at /admin. That prefix becomes req.baseUrl.
  2. Route is /new. Inside the router, req.path is /new (mount stripped).
  3. Keep the full client URL. req.originalUrl still includes path and query string.
  4. Use each for the right job. Prefix for links under the mount; path for local routing; originalUrl for logging.

Example 3 — One Router, Several Mount Paths

Mount the same router on two prefixes. req.baseUrl is whichever path actually matched.

Express
const express = require('express');
const app = express();
const greet = express.Router();

greet.get('/jp', (req, res) => {
  res.json({ baseUrl: req.baseUrl, message: 'Konnichiwa!' });
});

// Same router, two mount paths
app.use(['/greet', '/hello'], greet);

app.listen(3000);

How It Works

  1. Register one router twice. app.use(['/greet', '/hello'], greet) accepts either prefix.
  2. Match decides baseUrl. A hit to /greet/jp sets /greet; /hello/jp sets /hello.
  3. Not the pattern list. Unlike app.mountpath, req.baseUrl is the concrete matched string for this request.
  4. Same handler logic. The route /jp stays unchanged; only the mount prefix differs.

Runtime Support

req.baseUrl is an Express.js request property. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only send the URL; they do not see this property.

✓ Server · Express 4 & 5

req.baseUrl

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

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
req.baseUrl Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Mount a router, then log req.baseUrl to see the prefix.

Frequently Asked Questions

It is the URL path on which the current router instance was mounted — for example /greet when you call app.use('/greet', greetRouter).
req.baseUrl is the mount prefix. req.path is the remaining path inside the current router (the mount point is not included).
req.originalUrl is the full original request URL path (plus query). It is effectively a combination of req.baseUrl and req.url.
Yes. On top-level app routes that are not under a mounted router, req.baseUrl is usually ''.
It returns the matched string for that request, not the pattern list. Mounted on ['/greet', '/hello'], a hit to /hello/jp gives baseUrl '/hello'.

Next: req.body

Read parsed request body data from JSON or form submissions.

req.body 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