如何用node.js 获取访问者的公网IP?
request.connection.remoteAddress http://stackoverflow.com/questions/8107856/how-can-i-get-the-users-ip-address-using-node-js
request.connection.remoteAddress
顺便问一下,如何用最简单的非异步的方式获取本机ip地址
@a272121742 我只知道用 localhost 了… 求助楼下…
localhost
@a272121742 参考这里,万能谷歌啊 http://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js
我给你一个function,是目前我的网站正在使用的http://sogego.com
exports.getClientIP = function(req){ var ipAddress; var headers = req.headers; var forwardedIpsStr = headers[‘x-real-ip’] || headers[‘x-forwarded-for’]; forwardedIpsStr ? ipAddress = forwardedIpsStr : ipAddress = null; if (!ipAddress) { ipAddress = req.connection.remoteAddress; } return ipAddress; }
这个方法可以判断你是否使用了nginx和一般情况
@snoopy 那么多异步的方法, Node 设计得好邪门呐 非异步的话有么?
@jiyinyiyong @snoopy @a272121742 os.networkInterfaces() 参考这里:http://nodejs.org/api/os.html#os_os_networkinterfaces
Get a list of network interfaces:
{ lo0: [ { address: '::1', family: 'IPv6', internal: true }, { address: 'fe80::1', family: 'IPv6', internal: true }, { address: '127.0.0.1', family: 'IPv4', internal: true } ], en1: [ { address: 'fe80::cabc:c8ff:feef:f996', family: 'IPv6', internal: false }, { address: '10.0.1.123', family: 'IPv4', internal: false } ], vmnet1: [ { address: '10.99.99.254', family: 'IPv4', internal: false } ], vmnet8: [ { address: '10.88.88.1', family: 'IPv4', internal: false } ], ppp0: [ { address: '10.2.0.231', family: 'IPv4', internal: false } ] }
@leizongmin 喔… 秒杀了问题
@jiyinyiyong 你们这些家伙都不看API文档的。。。
@leizongmin 表示关于 OS 学得太散, 看过文档很多不懂对应什么
@leizongmin 原来0.8加上去了
https://github.com/alsotang/externalip
这个库
TJ的superagent都用了
@jiyinyiyong 这个只是获得本地的IP而已。
var ip = req.headers[‘x-forwarded-for’] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
用了 os.networkInterfaces() 依然获取不到 公网 IP 上图:
我去,三年前的讨论啊。 nodejs上的有tcp,有http要看哪一层需要知道访问者的来源IP。 @highsea , os.networkInterfaces 明显是看自己主机的IP情况吧。 5楼的应该像,http这一层。
CNode 社区为国内最专业的 Node.js 开源技术社区,致力于 Node.js 的技术研究。
request.connection.remoteAddresshttp://stackoverflow.com/questions/8107856/how-can-i-get-the-users-ip-address-using-node-js顺便问一下,如何用最简单的非异步的方式获取本机ip地址
@a272121742 我只知道用
localhost了… 求助楼下…@a272121742 参考这里,万能谷歌啊 http://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js
我给你一个function,是目前我的网站正在使用的http://sogego.com
exports.getClientIP = function(req){ var ipAddress; var headers = req.headers; var forwardedIpsStr = headers[‘x-real-ip’] || headers[‘x-forwarded-for’]; forwardedIpsStr ? ipAddress = forwardedIpsStr : ipAddress = null; if (!ipAddress) { ipAddress = req.connection.remoteAddress; } return ipAddress; }
这个方法可以判断你是否使用了nginx和一般情况
@snoopy 那么多异步的方法, Node 设计得好邪门呐 非异步的话有么?
@jiyinyiyong @snoopy @a272121742 os.networkInterfaces() 参考这里:http://nodejs.org/api/os.html#os_os_networkinterfaces
Get a list of network interfaces:
@leizongmin 喔… 秒杀了问题
@jiyinyiyong 你们这些家伙都不看API文档的。。。
@leizongmin 表示关于 OS 学得太散, 看过文档很多不懂对应什么
@leizongmin 原来0.8加上去了
https://github.com/alsotang/externalip
这个库
TJ的superagent都用了
@jiyinyiyong 这个只是获得本地的IP而已。
var ip = req.headers[‘x-forwarded-for’] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
用了 os.networkInterfaces() 依然获取不到 公网 IP 上图:
我去,三年前的讨论啊。 nodejs上的有tcp,有http要看哪一层需要知道访问者的来源IP。 @highsea , os.networkInterfaces 明显是看自己主机的IP情况吧。 5楼的应该像,http这一层。