如何确定节点中的用户IP地址

api Node.js

猿逆天GO

2020-03-18

如何确定控制器中给定请求的IP地址?例如(快递):

app.post('/get/ip/address', function (req, res) {
    // need access to IP address here
})

第2069篇《如何确定节点中的用户IP地址》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
米亚宝儿 2020.03.18

Had the same problem...im also new at javascript but i solved this with req.connection.remoteAddress; that gave me th IP address (but in ipv6 format ::ffff.192.168.0.101 ) and then .slice to remove the 7 first digits.

var ip = req.connection.remoteAddress;

if (ip.length < 15) 
{   
   ip = ip;
}
else
{
   var nyIP = ip.slice(7);
   ip = nyIP;
}
理查德乐 2020.03.18

如果使用快递...

req.ip

我一直在寻找,然后就像等待,我正在使用快递。咄。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android