All Steps to Get Full URL From Request in Express on Node.js

Robin
Updated on September 7, 2022

If we want to perform some functionalities based on the page a user is visiting, it is important to get the full URL in the Express server of that page. For example, if the request is not using HTTPS, we can redirect HTTP requests to HTTPS in Node.js and Express using that URL.

Express provides the protocol, hostname, and path of an URL through its request object inside any middleware function and routes. This object has protocol, hostname and url properties that can be used to construct and get the full URL of the current request.

We also have other alternatives other than these 3 properties. We will see all the possible ways to retrieve the complete URL from the request object in Express.

How to Get the Full URL in ExpressJS

In order to construct the complete URL in Express, we need 3 things i.e. protocol, hostname, and path. All of them are available in the Express request object as its properties.

All Steps to Get Full URL From Request in Express on Node.js

In the example, I will use the middleware function. But you can follow the same process inside any route. Because you have access to the same request object in the routes.

Getting The Protocol

          app.use((req, res, next) => {
    console.log(req.protocol);
    // http or https
    
    next();
});
        

To get the protocol, we can access the protocol property in the req object. If the request uses an SSL or TLS certificate, the req.protocol will return https otherwise http.

Getting The Hostname

You can get the host from the request header. When we access the req.headers.host property, we get the hostname in return.

          app.use((req, res, next) => {
    console.log(req.headers.host);
    // example.com
    // OR
    // www.example.com
    
    next();
});
        

The hostname means the domain name of a website. This property returns that domain name. If your domain name uses the www version, it will contain the www version of your domain.

With this property, you can check to redirect from non-www to www URL or www to non-www URL in Express if you want.

Also Read: Easy Ways to Redirect URL in Node.js (Express & HTTP Module)

But there are other ways to get the hostname in Express. Mainly you have 3 ways to access the hostname with a little difference. These are:

  • req.hostname
  • req.get("host")
  • req.headers.host

You can use any one of those. They all return the host value. But to understand the difference let's see an example.

URLhttps://example.com:443

If the domain contains a port number, the req.hostname property will return only the hostname without the port number. But the other two will return the port number as well with the hostname.

As I said before, you can use any one of those. Because domain names don't contain the port number nowadays. Therefore, all of them will give the same results.

Getting The Path

You have 2 options to get the URL path in the Express framework. You can use the req.url or req.originalUrl properties.

          app.use((req, res, next) => {
    // if the URL is https://example.com/about

    console.log(req.url);
    // /about

    console.log(req.originalUrl);
    // /about
    
    next();
});
        

As you can see, both of them return the same value. So, whatever property you use in your Express server, you will get the same result.

These properties return everything else after the hostname. If the URL contains any query string, you will also get that query string from these properties.

          app.use((req, res, next) => {
    // if the URL is https://example.com/about?username=max

    console.log(req.url);
    // /about?username=max

    console.log(req.originalUrl);
    // /about?username=max
    
    next();
});
        

Construct The Full URL

After getting all 3 parts from the request object, it is very easy to construct the full URL. You just have to place them one after another in the right order.

First, place the protocol name followed by the hostname, and at the end put the URL path. We will use the template literal syntax to join them.

          app.use((req, res, next) => {
    const fullUrl = `${req.protocol}://${req.headers.host}${req.url}`;

    console.log(fullUrl);
    // https://example.com/about

    next();
});
        

In this example, I am getting the complete URL by joining those 3 properties. You can use this URL in your application according to your requirements.

Conclusion

You have seen all the steps to retrieve an URL from the request object in Express. Get the request protocol, hostname, and path. Then join them together to construct the URL.

I hope you got a clear idea about this topic and you will be able to get the full URL from a request in Express and Node.js server.

Related Posts