devnotes.

req.baseUrl Property

req.baseUrl returns the path on which a router instance is mounted — useful for figuring out where the current router "lives" inside the larger app.

Syntax

req.baseUrl

Example

const express = require("express");
const app = express();
const PORT = 4700;

const vendor = express.Router();

vendor.get("/dashboard", (req, res) => {
    console.log(req.baseUrl); // /vendor
    res.send("Vendor Dashboard");
});

app.use("/vendor", vendor);

app.listen(PORT, () => console.log(`Listening on ${PORT}`));

A GET request to /vendor/dashboard logs /vendor.

Use cases

  • Identifying where a router is mounted.
  • Building dynamic links based on the router's mount path.
  • Debugging routing structures in larger apps with nested routers.
req.baseUrl Property | DevNotes