Reputation: 11307
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....");
Upvotes: 15
Views: 54120
Reputation: 547
req.connection.remoteAddress
req.connection.remotePort
req.connection.localAddress
req.connection.localPort
Upvotes: 3
Reputation: 2223
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 );
Upvotes: 23
Reputation: 2055
Assuming you want the address your server is bound to, you can use server.address.address
to get the IP address the server is bound to. Similarly, you can use server.address.port
to get the port number that the server is bound to.
From: http://nodejs.org/api/net.html#net_server_address.
Upvotes: 3