Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

I've a following very basic code of HTTP server which is listening on port 8000. How to determine the IP address of server, can it be retrieved from the 'server' variable? I am working on an application where I need to automatically send the server Info (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....");
like image 529
Sohaib Avatar asked Jun 24 '14 04:06

Sohaib


1 Answers

Use the following:

server.address()

Which logs something like:

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

To log the IP of your server to the console, use:

console.log( server.address().address );
like image 84
f1lt3r Avatar answered Sep 19 '22 18:09

f1lt3r