req.path

The path portion of the request URL, excluding the query string.

Since Express 4 Spec ↗

Syntax

req.path

Returns

string — The URL pathname.

Examples

// GET /users/7?tab=posts
app.use((req, res, next) => {
  console.log(req.path);
  next();
});
Output
/users/7
app.use((req, res, next) => {
  if (req.path.startsWith('/admin')) return res.sendStatus(403);
  next();
});
Output
$ curl localhost:3000/admin/x
(403 Forbidden)

Notes

`req.path` excludes the query string (use `req.query` for that) and the host. Inside a mounted router it is relative to the mount point; use `req.originalUrl` for the full original path including the query.

See also