Node.js:获取客户端的IP [英] Node.js: Get client's IP
问题描述
req.connection.remoteAddress,req.headers ['x-forwarding-for'],req.ip,req.ips,这是什么意思?
req.connection.remoteAddress, req.headers['x-forwarded-for'], req.ip, req.ips, what does it all mean?
有没有一个直接的方法来简单地获取客户端/用户代理的IP地址,请求到我的站点在Node.js / Express?我不理解所有的代理东西,或者req对象的所有属性之间的所有区别。此外,我不明白Express的信任代理选项是什么。
Is there a straight forward way to simply get the IP address of the client/user-agent making the request to my site in Node.js/Express? I'm not understanding all the proxy stuff or what all the differences between all the properties of the req object. Also, I don't understand what the 'trust proxy' option is for Express.
有人可以直截了当地解释所有这些之间的区别属性,并回答我如何只是简单地得到客户端的IP?
Could someone give me a straight forward explanation to what the difference is between all of these properties, and answer how I can just simply get the client's IP?
推荐答案
req.ip
是在Express中获取客户端IP地址的简单方式。您可以看到它使用的逻辑(其涉及从代理地址
req.ips
的数组中获取第一个项目,其中该数组由 x-forwarding-for
标头)此处。
req.ip
is the straightforward way to get the client's IP address in Express. You can see the logic it uses (which involves grabbing the first item from the array of proxy addresses req.ips
, where that array is constructed from the x-forwarded-for
headers) here.
这篇关于Node.js:获取客户端的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!