6

At present I'm using req.headers.host to determine the server's host name, which is effective when processing a request received at the server.

But how can one determine the host name in the absence of a request (i.e. without req)? I'm thinking, in particular, when the node server is first created and run, and before any requests are received.

I'm using the express module.

Thanks

drmrbrewer
  • 11,491
  • 21
  • 85
  • 181
  • http://stackoverflow.com/a/9399767/2182767 - check this answer – Andrey Aug 17 '15 at 14:09
  • 1
    If there is no request, you don't actually know the hostname. A single server can serve multiple hostnames. – SLaks Aug 17 '15 at 14:11
  • I only have low-level experience of Cloud 9 IDE and heroku, and in both of those (at least to the level I'm working) you access the node server via a specific URL. So with C9 when you start the server it says "Your code is running at https://xxx.yyy.com", while with heroku you're assigned a specific URL for your server. So, when I start the same server on C9 and on heroku, how do I know whether it's running on C9 or heroku? I had hoped I could do this just by checking the hostname, like I can do on receiving a request. – drmrbrewer Aug 17 '15 at 17:28

1 Answers1

0

you can use the os module and os.hostname()

https://nodejs.org/docs/latest/api/os.html#os_os_hostname

cdurth
  • 345
  • 1
  • 4
  • 12