QA
Beginner LevelExpress JS

What is the exact difference between req.path, req.url, and req.originalUrl?

❓ Interview Prompt: Compare req.path (pathname only), req.url (rewritten inside routers), and req.originalUrl (unaltered client URL).

Express provides three properties representing request URLs:

1. 'req.path':

•
Contains ONLY the URL pathname, excluding query strings ('/users').

2. 'req.url':

•
Contains the path plus query strings. Note: Inside a mounted 'express.Router()', 'req.url' is rewritten to be relative to the router mount point!

3. 'req.originalUrl':

•
Retains the full original client URL (path + query strings) completely unaltered by sub-router mount points!

Code Implementation Example

javascript
import express from 'express';
const app = express();
const router = express.Router();

// Request URL: GET /api/v1/users/42?details=true
router.get('/users/:id', (req, res) => {
  console.log(req.path);        // '/users/42' (Pathname only)
  console.log(req.url);         // '/users/42?details=true' (Router relative)
  console.log(req.originalUrl); // '/api/v1/users/42?details=true' (Full original URL!)
  res.send('URL Inspection');
});

app.use('/api/v1', router);
💡Comparing req.path, req.url, and req.originalUrl inside mounted sub-routers.
🎯 Interview Key Takeaway

Use req.originalUrl to inspect the full unaltered client request URL including query strings.

#req.originalUrl#req.path#req.url#Beginner
Back to all Express JS Questions→

More Express JS Interview Questions

Beginner LevelExpress JS

What is Express.js and how does the middleware pipeline work?

Beginner LevelExpress JS

What is the difference between req.params, req.query, and req.body?

Beginner LevelExpress JS

How does express.Router() work for building modular route controllers?