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

18 Replies, 1636 Views

Proxies are a pain, right? For the nodejs request source hostname, you gotta check multiple headers.

Try this:
```javascript
const host = req.headers['x-real-ip'] || req.headers['x-forwarded-for'] || req.connection.remoteAddress;
```

If you’re using NGINX, make sure it’s passing the right headers.

Also, the `request-ip` package is handy for this: [request-ip npm](https://www.npmjs.com/package/request-ip).

Messages In This Thread



Users browsing this thread: 1 Guest(s)