How to Get the Request Source Hostname in Node.js? or Node.js Request Source Hostname: How to Retriev

18 Replies, 1715 Views

Hey! Yeah, `req.headers.host` is a good start, but it’s not always reliable, especially with proxies.

For the nodejs request source hostname, you might wanna check `req.headers['x-forwarded-host']` if you're behind a load balancer.

Also, the `host` header can be spoofed, so if security’s a concern, look into validating it.

Here’s a quick snippet:
```javascript
const host = req.headers['x-forwarded-host'] || req.headers.host;
console.log('Host:', host);
```
Hope that helps!

Messages In This Thread



Users browsing this thread: 1 Guest(s)