Express req.path Property
What you’ll learn
- What value
req.pathcontains in Express requests. - How it differs from
req.urlandreq.originalUrl. - How to use path-only checks in middleware.
- How query strings are handled separately via
req.query.
Usage syntax
javascript
req.path
req.query1
Read clean pathname from request
javascript
app.get('/search', function (req, res) {
res.json({
path: req.path,
query: req.query
});
});2
Filter middleware by path prefix
javascript
app.use(function (req, res, next) {
if (req.path.indexOf('/api/') === 0) {
console.log('API request:', req.path);
}
next();
});❓ FAQ
It is the pathname portion of the request URL, without query parameters.
req.path is path-only, while req.originalUrl contains path plus query string as originally received.
No. Query values are available through req.query, not req.path.
req.url may include query and can be rewritten in middleware chains; req.path is normalized pathname.
Use it for path-based checks, analytics grouping, middleware filters, and logging clean route paths.
Did you know?
req.path returns only the URL pathname (without query string), which is useful for clean route checks and logging.
4 people found this page helpful
