如何确定节点的用户的IP地址 [英] How to determine a user's IP address in node

查看:201
本文介绍了如何确定节点的用户的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能确定给定请求的IP地址从控制器内?例如(在EX preSS):

  app.post('/获取/ IP /地址',函数(REQ,RES){
    //需要访问的IP地址在这里
})


解决方案

在你的要求对象有一个叫做物业连接,这是一个 net.Socket 对象。该net.Socket对象有一个属性 remoteAddress ,因此,你应该能够与这个调用来获取IP:

  request.connection.remoteAddress

请参见 HTTP 并的net

修改

由于@juand在评论中指出,正确的方法来获得远程IP,如果服务器是一个代理之后,为 request.headers ['X-转换']

How can I determine the IP address of a given request from within a controller? For example (in express):

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

解决方案

In your request object there is a property called connection, which is a net.Socket object. The net.Socket object has a property remoteAddress, therefore you should be able to get the IP with this call:

request.connection.remoteAddress

See documentation for http and net

EDIT

As @juand points out in the comments, the correct method to get the remote IP, if the server is behind a proxy, is request.headers['x-forwarded-for']

这篇关于如何确定节点的用户的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆