Express.js req.path Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

In Express.js, req.path is a string with the pathname of the request URL. For /users?sort=desc, it is '/users' — the query string is left out.

Use it for clean path checks and logging. Query values belong on req.query. For the full original path + query, use req.originalUrl. See also req.baseUrl when middleware is mounted.

Syntax

Read req.path in your Express.js route handler. Start with this line.

Express
req.path
CallWhat it does
req.pathPrimary signature.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

req.path

Parameters

This Express.js property is read from the request or response — it has no call parameters.

Return Value

req.path is a string property: the pathname of the request URL within the current mount, without the query string.

Express
// GET /admin/new?x=1 mounted at /admin
req.path;        // '/new'
req.originalUrl; // '/admin/new?x=1'

Express.js Examples

Example 1 — Pathname Without Query

Official shape: query stays on req.query; req.path stays clean.

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

app.get('/users', (req, res) => {
  // GET /users?sort=desc
  console.dir(req.path);
  // => '/users'
  res.json({ path: req.path, query: req.query });
});

app.listen(3000);

How It Works

  1. Client sends path + query. Example: /users?sort=desc.
  2. req.path drops ?… Result is '/users'.
  3. req.query holds filters. Here { sort: 'desc' }.
  4. Respond with both. Makes the split obvious while learning.

Example 2 — Path Inside Mounted Middleware

Same numbers as the Express docs under /admin.

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

// GET http://www.example.com/admin/new?sort=desc
app.use('/admin', (req, res, next) => {
  console.dir(req.originalUrl); // '/admin/new?sort=desc'
  console.dir(req.baseUrl);     // '/admin'
  console.dir(req.path);        // '/new'
  next();
});

app.get('/admin/new', (req, res) => {
  res.send('ok');
});

app.listen(3000);

How It Works

  1. Mount at /admin. Express rewrites the internal URL for that stack.
  2. originalUrl stays full. Includes mount and query.
  3. baseUrl is the mount. Here /admin.
  4. path is the remainder pathname. /new — no query, no mount.

Example 3 — Filter Middleware by Path Prefix

Run extra logging only for API paths, ignoring query noise.

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

app.use((req, res, next) => {
  if (req.path.startsWith('/api/')) {
    console.log('API request:', req.path);
  }
  next();
});

app.get('/api/items', (req, res) => {
  res.json({ ok: true });
});

app.get('/home', (req, res) => {
  res.send('home');
});

app.listen(3000);

How It Works

  1. Check pathname only. startsWith('/api/') ignores ?page=2.
  2. Log when it matches. API traffic is easy to spot.
  3. Always call next(). Filtering must not block other routes.
  4. Non-API paths skip the log. /home continues quietly.

Runtime Support

req.path is an Express.js request property. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers do not execute it — they only send HTTP requests to your app.

✓ Server · Express 4 & 5

req.path

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
req.path Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It is a string with the pathname part of the request URL — no query string. For /users?sort=desc, req.path is '/users'.
No. Query values live on req.query. req.path is pathname only.
Inside mounted middleware, the mount point is not included. Under app.use('/admin', …) for /admin/new, req.path is '/new'.
req.originalUrl keeps the full original path plus query. req.path is the current pathname without query (and without the mount prefix in mounted middleware).
No. It is a server-side Express request property. Browsers only send the HTTP request; your app reads req.path on the server.

Next: req.protocol

Read whether the request is http or https (including proxy-aware behavior).

req.protocol tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful