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

18 Replies, 1641 Views

If you’re using Express, `req.hostname` is the way to go for nodejs request source hostname.

It handles the `host` header and ignores port numbers, which is nice.

But if you’re behind a proxy, set `trust proxy` in Express:
```javascript
app.set('trust proxy', true);
```
Then `req.hostname` will respect `x-forwarded-host`.

Messages In This Thread



Users browsing this thread: 2 Guest(s)