Skip to content

How to get request path from an Express request?

In this small post, let’s have a look at how you can extract the request path from an Express request object.

Let’s assume you are building an API with Express but you want to log the request path of the API so that it would be helpful for debugging purposes. Or you would like to use the path for redirecting.

req.originalUrl

req.originaUrl property returns the request path. It retains the original request URL path which would be really helpful for rerouting purposes.

If you have an API with path /api/login, req.originalUrl property would have /api/login

app.use("*", function(req, res, next){
  console.log("Path -- " + req.originalUrl);
}
        
//Path -- /api/login

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.