Explain Codes LogoExplain Codes Logo

How to get request path with Express req object

javascript
express
req-object
url-parsing
Nikita BarsukovbyNikita Barsukov·Jan 12, 2025
TLDR

Quickly fetch the path of a client's request in Express with req.path. This method filters out the pathname from the request URL where the route matched.

Example:

app.get('*', (req, res) => { console.log(req.path); // Outputs: "/example", not "/example?filtering=Nn4KDw" });

In the code above, it logs the direct route accessed, /example, leaving out the hostname and query parameters.

On the dark side, what if you have the hunger for the full URL path with all its trimmings—the query string, route parameters and the rest? Popping req.originalUrl into your code, as shown below, is the secret sauce.

app.get('*', (req, res) => { console.log(req.originalUrl); // Outputs: "/example?filtering=Nn4KDw", not just "/example" });

Exploring the req object

Mastering the Express req object requires understanding its powers. The following sections unlock those powers for you.

Using req.originalUrl

The req.originalUrl property piece of magic spits out the entire path of the request, garnished with the query string.

app.use((req, res, next) => { console.log(req.originalUrl); // Outputs: "/example?filtering=Nn4KDw", guess who's full now? next(); });

Joining req.baseUrl with req.path

When your code is dealing with Express sub-apps or router modules, merge req.baseUrl with req.path to deliver wholesome paths.

app.use('/subapp', subApp); subApp.get('/example', (req, res) => { console.log(req.baseUrl + req.path); // Outputs: "/subapp/example", that's "/example" on steroids });

Tackling authentication and redirection

Interesting authentication or redirection scenarios call for req.originalUrl as it maintains the precious query string, guiding users back to their intended path after login.

function authMiddleware(req, res, next) { if (!req.session.user) { // Whoops! No ticket (session), redirecting... res.redirect('/login?redirect=' + encodeURIComponent(req.originalUrl)); // Back to you after the login ad break! return; } next(); }

Inside the req object

Knowing your req properties in Express upgrades your URL handling game. Here's the cheat sheet:

  • req.path: Fetches the path, with query string missing.
  • req.originalUrl: Yields the complete request path with query string intact.
  • req.url: Similar to req.originalUrl but malleable by Express middleware.
  • req.baseUrl: Delivers the prefix path if the route is under a sub-path.

Modern techniques for URL parsing

ExpressJS champions robust URL parsing for smarter app navigation and routing. The following sections unleash these modern techniques.

Fetching the protocol and hostname

The req object also hands over the protocol and hostname. The commented example below knows.

console.log(req.protocol); // Outputs: "http", no "https" just yet, hold on console.log(req.hostname); // Outputs: "example.com", now that we've cleared customs

In combination with req.originalUrl, you can reconstruct the entire URL, a full trip back to the client's request.

Adapting to complex, dynamic routes

With our dynamo req properties, you can now manage middleware better and stay on track when URL rewriting happens.

Handling modular applications

Understanding how Express routers and modular applications behave, you can stay on top of path changes due to req.baseUrl updates.

Precautions with older methods

While romancing the complexity of URL parsing methods, be wary of outdated approaches. Only dance with current Express versions and best practice guidelines.