Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用Restify从请求对象获取客户端IP?_Node.js_Restify - Fatal编程技术网

Node.js 如何使用Restify从请求对象获取客户端IP?

Node.js 如何使用Restify从请求对象获取客户端IP?,node.js,restify,Node.js,Restify,我很难找到如何从路由访问REST客户端的IP地址 server.get('api/foo', function(req, res, next) { // How can I access the IP address of the requester from here? } 这起到了作用: req.connection.remoteAddress其他答案在代理后不起作用,在这些情况下,您将获得代理服务器地址 req.headers['x-forwarded-for'] || req.

我很难找到如何从路由访问REST客户端的IP地址

server.get('api/foo', function(req, res, next) {
    // How can I access the IP address of the requester from here?
}
这起到了作用:


req.connection.remoteAddress

其他答案在代理后不起作用,在这些情况下,您将获得代理服务器地址

req.headers['x-forwarded-for'] || req.connection.remoteAddress;

如果代理将原始IP设置在
x-forwarded-for
头中,那么代理将在代理后工作,这在默认情况下是可以做到的,您可以很容易地添加到nginx之类的东西。

尝试检查
req
对象。x-forwarded-for可以包含一个转发主机链。您应该在coma分离器上拆分它并获得第一个(最左侧)IP。或者第一个(最左边的)公共IP,如果您需要它来进行地理定位。