【发布时间】:2020-02-11 09:10:27
【问题描述】:
我在 heroku 上运行了这个 node/express 应用程序。我需要保存访问者的 IP 地址。
我已在应用程序中将信任代理设置为 true
https://expressjs.com/en/guide/behind-proxies.html
app.set('trust proxy' , true);
然后在我的端点中,我从请求对象中检索 ip 地址作为 req.ip
app.post('/api/send-data', (req, res) => {
let data;
// find the user's ip address
const ip = req.ip;
console.log(request came from ${ip});
}
工作正常,但问题是 req.ip 返回 ipv6 或 ipv4 而我只想保存用户的 ipv4,我该怎么做?
我已阅读 node/express 文档并将我的应用设置为接收来自 ipv4 的请求
app.listen(port, 0.0.0.0)
但我仍然看到来自 IPV6 地址的请求。是不是因为请求在到达应用程序之前先到达 heroku 路由器,并且 heroku 路由器在 x-forwarded 标头中传递用户的 ip,所以应用程序总是认为它是来自 ipv4 的请求?如果是这样,有什么办法可以解决这个问题?
【问题讨论】: