req.url / req.originalUrl
The request URL; originalUrl preserves the full path even inside mounted routers.
Syntax
req.url / req.originalUrl Returns
string — The URL path and query string.
Examples
// GET /api/users?active=1 mounted at /api
const router = express.Router();
router.get('/users', (req, res) => {
res.json({ url: req.url, original: req.originalUrl });
});
app.use('/api', router);
Output
{"url":"/users?active=1","original":"/api/users?active=1"}
app.use((req, res, next) => {
console.log(req.originalUrl);
next();
});
Output
/api/users?active=1
Notes
`req.url` is rewritten relative to the router mount point;
`req.originalUrl` always holds the unchanged full URL - use it for
logging and redirects. Both include the query string, unlike
`req.path`.