Node.js How to get the IP address of an HTTP server listening on a specific port

I have the following very simple HTTP server code that listens on port 8000. How can I determine the IP address of a server, can I get it from the "server" variable? I am working on an application where I need to automatically send the Info server (ip, port, etc.) to redis store.

I'm new to node.js, thanks for the help

var http = require("http"); var server = http.createServer(function(request, response) {   response.writeHead(200, {"Content-Type": "text/html"});   response.write("Hello!!!");   response.end(); }); 

 server.listen(8000); console.log("Server is listening...."); 
+10
source share
3 answers

Use the following:

 server.address() 

Which writes something like:

 { address: '0.0.0.0', family: 'IPv4', port: 8080 } 

To register the IP address of your server in the console, use:

 console.log( server.address().address ); 
+16
source

Assuming you need the address that your server is bound to, you can use server.address.address to get the IP address that the server is bound to. Similarly, you can use server.address.port to get the port number on which the server is bound.

From: http://nodejs.org/api/net.html#net_server_address .

+2
source
 req.connection.remoteAddress req.connection.remotePort req.connection.localAddress req.connection.localPort 
0
source

All Articles