I have an express app running under Node.js v6.9.4, serving both via HTTP and HTTPS.
I've noticed a few times that requests coming in have had blank remoteAddress data.
Under what conditions can a request have no / blank remoteAddress?
答案 0 :(得分:4)
remote address is blank when behind a proxy
https://github.com/hapijs/hapi/issues/1210
you can use
const ip = req.headers['x-forwarded-for'] || (req.connection && req.connection.remoteAddress) || ''
答案 1 :(得分:0)
虽然如果未设置应用程序变量信任代理,应用程序将无法运行,但除非配置了信任代理,否则它将错误地将代理的IP地址注册为客户端IP地址。
获取IP地址的替代解决方案由@ trushar-gupta
提供您还可以了解有关 Express代理的更多信息 here