简体   繁体   中英

Get client domain in request

I have public GCP cloud function:

exports.helloWorld = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!';
  console.log(req.headers.origin);
  res.status(200).send(message);
};

and I need to get domains of clients, which will call this function. 'req.headers.origin' returns 'undefined' and 'req.headers.host' returns link of my cloud function.

When your function is called, the req parameter contains the Request object from Express. The documentation for this can be found here . This object contains a field called ip which is the IP address of the caller.

We will also find the same IP address in the header x-forwarded-for .

We won't find the domain name (eg. xyz.com) in the data. I think the best you will find is the callers IP address. You may be able to perform an IP address lookup using DNS... see the reverse() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM