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


Some times, We need to get Referer http header in Node application. This post explains about how to find the referer url in Nodejs applicaiton. 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 contains domain or a page URL making request. some times Referer is also called referrer as old browsers still reference this. We can use any one of this.

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

In request header, You will see the following value

Referer: https://website1.com

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

How to find the referer page url in nodeJS application

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

if you are using express version less than 4.x

You can directly call below method

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

In expression 4.x version

you can get using request.get method

Referrer is an case insensitive header

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

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

Conclusion

You learned how to retrieved return url in node express application. This header value is controlled by client, This can be modified and changed to different value, So it is not better to use logic based on value. However logging and analytics purpose we can consider

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.