Nodejs Node Name

Ok, so in Node.js, it's pretty easy to get the hostname of a request made on my server:

app.get('/', function(req,res){ console.log(req.headers.host); }); 

Is there an easy way to determine the hostname of my actual HTTP server? For example, my server is running at http://localhost:3000 - can I programmatically determine this address? I am using expressjs.

+7
source share
1 answer

Yes you can use;

 var express = require('express'), app = express(), server = require('http').createServer(app); server.listen(3000, function(err) { console.log(err, server.address()); }); 

should print

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

you can also restore the hostname for os as follows:

 require('os').hostname(); 
+12
source

All Articles