简体   繁体   English

nodejs-知道服务器从哪个IP地址获取数据

[英]nodejs - know from which ip address the server makes the get

Sorry, I'm quite new to network programming and nodejs. 抱歉,我是网络编程和nodejs的新手。 I'm using nodes in a server with some clients in a local network. 我正在使用服务器中的节点以及本地网络中的某些客户端。

Sometimes I have to ask data to clients via get request: 有时我必须通过get请求向客户询问数据:

// somewhere inside server.js
function askDataToClient(ip) {

    var options = {
        host: String(ip),
        port: 80,
        path: '/client/read',
        auth: 'username:password'
    };

    var request = http.get(options, function(htres){
        var body = "";
        htres.on('data', function(data) {
            body += data;
        });
        htres.on('end', function() {
            body = JSON.parse(body);
            res.json(body);
            res.end();
        })
        htres.on('error', function(e) {
            // ...
        });
    });
}

I'd like to know the server ip used for calling this get request. 我想知道用于调用此get请求的服务器ip。

I know of this answer but it gives me all the various network active on the server machine: 我知道这个答案,但是它为我提供了服务器计算机上所有活动的网络:

lo0 127.0.0.1
en1 192.168.3.60
bridge0 192.168.2.1

If I'm querying the client 192.168.3.36 I know it is the second ip, 192.168.3.60 because they are on the same network.. but How to know it programmatically? 如果我正在查询客户端192.168.3.36,我知道它是第二个IP,即192.168.3.60,因为它们位于同一网络上。但是如何以编程方式知道它?

您应该能够使用htres.socket.address().address来获取IP。

Check out request.connection.remoteAddress property available for the HTTP Request object. 签出可用于HTTP Request对象的request.connection.remoteAddress属性。 This indicates the address of the remote host performing the request. 这指示执行请求的远程主机的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM