I'm using express.js and i need to know the domain which is originating the call. This is the simple code
app.get(
'/verify_license_key.json',
function( req, res ) {
// do something
How do i get the domain from the req
or the res
object?
I mean i need to know if the api was called by somesite.com or someothersite.com.
I tried doing a console.dir of both req
and res
but i got no idea from there, also read the documentation but it gave me no help.
This question is related to
javascript
node.js
express
Instead of:
var host = req.get('host');
var origin = req.get('origin');
you can also use:
var host = req.headers.host;
var origin = req.headers.origin;
Recently faced a problem with fetching 'Origin' request header, then I found this question. But pretty confused with the results, req.get('host')
is deprecated, that's why giving Undefined
.
Use,
req.header('Origin');
req.header('Host');
// this method can be used to access other request headers like, 'Referer', 'User-Agent' etc.
In Express 4.x you can use req.hostname
, which returns the domain name, without port. i.e.:
// Host: "example.com:3000"
req.hostname
// => "example.com"
Source: Stackoverflow.com