How to get the full URL in Express
In this tutorial, we will learn about how to get the full url in the express route handler function.
Consider, we have the following url.
http://fun.com/users/sai
And our express route handler looks like this.
app.get('/users/sai',(req,res)=>{
})
Now, we need to get the full url inside our router handler function, instead of url path /users/sai
To do that, first we need to access the protocol, hostname, and path separately and join them all together to get the full url.
Here is an example:
app.get('/users/sai',(req,res)=>{
const protocol = req.protocol;
const hostname = req.host;
const path = req.originalUrl;
const fullURL = protocol + "://" + hostname + path; console.log(fullURL); // http://fun.com/users/sai
})