How do I find the a referring sites URL in node?
I'm using express, would I find this in the headers on connect or something?
Thanks!
In express 4.x:
req.get('Referrer')
This will also check both spellings of referrer so you do not have to do:
req.headers.referrer || req.headers.referer
Here is the documentation
Referer and I find req.get('Referrer') and req.get('Referer') get the same result
If you mean how do you get it when running an express server, then it's done using the header method on your request:
req.headers.referer;
// => "http://google.com"
undefined. This is in express router.
Commented
Aug 26, 2022 at 22:58
Express 5.0-alpha2 with new JS features.
const referrer = req.get('Referer') ?? req.get('Referrer') ?? null;
I am checking for both spelling as weirdly they can happen sometimes, but it should be super rare.
weirdly just access headers object didn't work for me. It's possible it's only issue with my alpha, but leaving it here for others.
For people having undefined / null
First of all: Referer is not always there. Actually, it's very often not there so make sure you have a fallback. There's a lot of reasons. It can be because someone visited link directly, but it can be also due to privacy settings, browser extensions etc.
Referer can also be also easily spoofed, so using it as a security measure is not really great.
For those cases where it's not empty it can be pretty useful for getting more info about users. Just remember to keep fallback.