ladar
ladar

Reputation: 5876

Node.JS and Express: get application host without request

I have running Node.JS & Express application on localhost (127.0.0.1) and I need to find out the current host (domain) without making requests (for cron job that starts when server is created). The server is created this way

   app = express.createServer();
   app.listen(PORT);   

Now I've found following solution

   app.address();

However the host returned by this is 0.0.0.0, port is returned correct so I guess I need to somehow specify the host during the server start. How can I force it to get the correct host? I've tried

  app = express.createServer({host: '127.0.0.1'});

but that doesn't work :(

Upvotes: 4

Views: 10048

Answers (3)

Oleg Imanilov
Oleg Imanilov

Reputation: 2751

Host may be reached by more than one Ip, you can find good example how to get it - at following article Get local IP address in node.js

Upvotes: 0

fent
fent

Reputation: 18205

You cant. You have to make a request to something like http://whatismyip.org/

But the reason you want it for can be changed. There are some modules that can be used instead of an actual cron job to do what you want.

https://github.com/ncb000gt/node-cron

Upvotes: 0

thejh
thejh

Reputation: 45568

http://nodejs.org/docs/latest/api/os.html#os.hostname

os.hostname()

Returns the hostname of the operating system.

Looks like it might be what you're looking for.

Upvotes: 3

Related Questions