How to get return url in nodejs application|http referrer header example

Sometimes, We need to get the Referer HTTP header in the Node application. This post explains how to find the referer URL in the Nodejs application. This tutorial describes all the steps to retrieve referer in javascript

[Referer](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.36) is an HTTP optional header in a request that contains a domain or a page URL making the request. some times Referer is also called referrer as old browsers still reference this. We can use any one of these.

Suppose In a website1.com, You have a link pointing to a different website2.com, and website2 contains Referer as website.com

In the request header, You will see the following value

Referer: https://website1.com

if you directly access the website, the referrer value is undefined.

How to find the referer page url in the nodeJS application

In Nodejs, Express library HTTP headers are retrieved using the request object.

if you are using an express version less than 4. x

You can directly call the below method

console.log(req.headers.referer) // website1.com

In expression 4. x version

you can get using request. get method

A referrer is a case-insensitive header.

console.log(req.get('Referrer')) // website1.com

This method checks for the Referer and Referrer header in a request

Conclusion

To Sum up,

In the node express application, you learned how to get the return URL. The client can change this header value. Because this can be changed to a different value, it is not a good idea to utilize logic based on value. However, we can consider logging and analytics purposes.