NodeJS gets the IP address used by remoteAddress

I have 3 simple NodeJS usign NET, HTTP and UDP servers. Each server listens on port X, but has several IP addresses. I would like to get the actual IP address of the server when the client connects to the server (the IP address to which the client is connected, the IP address that the client had to write to connect to the server).

var httpService = http.createServer(function(req, res){
   req.getActualServerAddress();
});
httpService.listen(8000);

var netService = net.createServer(function(socket) {
   socket.getActualServerAddress();
});
netService.listen(8001);

var udpService = dgram.createSocket("udp4");
udpService.on("message", function (msg, rinfo){
   rinfo.getActualServerAddress();
});
udpService.bind(8002);

thank.

+3
source share
1 answer

, 0.0.0.0. , [ Maqurading]. HTTP HTTP Host [ HTTP/1.1], .

:

socket.address()

, . , . { "address": "192.168.57.1", "port": 62053}

tcp:

var netService = require('net').createServer(function(socket) {
 address = netService.address();
 console.log("Stream on %j", socket.address());
 console.log("opened server on %j", address);
});
netService.listen(8001);

http:

var httpService = require('http').createServer(function(req, res){
console.log("Stream on %j", req.connection.address());
    res.end("Hi");
});
httpService.listen(8000);
+8

Source: https://habr.com/ru/post/1648161/


All Articles