Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Pipe

req.path

req.path is a string containing the path portion of an Express request URL. It excludes the query string, and mounted middleware sees the path without its mount point.

How it works

req.path is a string property on the request object that reports the path portion of the incoming URL. It excludes the query string, and middleware mounted at a path sees the path after that mount point rather than the mount point itself.

Syntax

req.path

Returns

String containing the path part of the request URL.

Key points

  • req.path exposes the URL's path portion as a string.
  • For a URL such as example.com/users?sort=desc, the path portion is /users.
  • The property can be inspected directly with console.dir(req.path).

Common mistakes

  • Mounted middleware receives a path with its mount point removed, so req.path may differ from the original URL path.

Example: Read the request path

const express = require('express');
const app = express();

app.get('/users', (req, res) => {
  res.json({ path: req.path });
});

app.listen(3000);

Run the app and request /users?sort=desc; the response contains /users, without the query string.

When to use it

Branch behavior by request path

Route-aware middleware

Record the URL path without query data

Request logging

Group requests by path component

Path-based metrics

Interview questions

QWhat does req.path contain?

It is a string containing the path part of the request URL. For /users?sort=desc, its value is /users.

Follow-up: How would you obtain the query string values? Use the request's query-related API, such as req.query, rather than req.path.

QDoes req.path include query parameters?

The query string is not part of req.path; only the URL path is represented. Therefore /users?sort=desc produces /users.

Follow-up: What happens in mounted middleware? The mount point is omitted from the value visible there.

QWhat caveat applies when reading req.path in middleware?

It is affected by middleware mounting: the mount point is not included when the property is read from that middleware. This reflects the path remaining after the middleware's mount location.

Follow-up: Why should middleware account for this? Path checks inside mounted middleware must use the shortened path rather than the application's original full path.

  • req.url
  • req.originalUrl
  • req.baseUrl
  • req.query
  • req.params

Source: Request Object - Express.js 4.x. Checked 2026-10-15.