74

I'm still learning Node JS and javascript and have an app. I have a configuration file where I need to grab the hostname of the server on Ubuntu 12.04

I tried something like:

 window.location.hostname

But that did not work. Sample code below:

exports.config = {
    app_name : [ window.location.hostname ]
}

If I use a string, it will load fine, but this is going to be managed through Github and needs to be differentiated when the app loads.

nocode
  • 1,238
  • 1
  • 15
  • 21
  • 1
    Try http://davidcaylor.com/2012/05/24/finding-the-hostname-in-node-js/ – Mörre Dec 12 '13 at 20:45
  • 1
    Possible duplicate of [Get hostname of current request in node.js Express](https://stackoverflow.com/questions/7507015/get-hostname-of-current-request-in-node-js-express) – Vince Mar 23 '18 at 23:10

3 Answers3

155

According to the node.js documentation for the "os" module you need to load the "os" module, which has a hostname() function:

var os = require("os");
var hostname = os.hostname();

However, that only is the hostname - without the domain name (the FQDN). There is no easy way to get the FQDN. You could use the node.js DNS functions to try to turn the IP address of the server (which you get with os.networkInterfaces(), see doc link above) into a name. The only problem is servers may have different interfaces and names, so you have to make a decision about which one you want.

You tried using the window object, but that only exists in the JavaScript runtime environment of browsers. Server side JavaScript doesn't have windows, obviously, so there is no window object. See this question: "Does node.js have equivalent to window object in browser".

With this information your question is a little strange - in the browser window.location.hostname is the host part of the URL the current page was loaded from. How do you translate that to a server context? The code you run on node.js is from that very server, by definition, so you don't actually need this information. You (may) need it in the browser because that information is variable, especially when you run mashups (JS code from various sources) your code may not know where the page it runs on was loaded from. On the server you always know it's your local filesystem.

By the way, you can always use localhost as the hostname :)

Community
  • 1
  • 1
John Smith
  • 1,724
  • 1
  • 9
  • 7
  • I should have clarified. I need this name to send data to a remote source and need to differentiate which server it's coming from. If you run the command 'hostname' in linux, it will return the server name. I want this name to be passed into my config so that when it sends data, I can organize by the groups of servers running it. – nocode Dec 13 '13 at 14:48
  • 1
    @nocode You haven't clarified anything, because if you use `os.hostname()` as described above you get the hostname, same as with the command line command `hostname`. If you need the remote host in an HTTP connection your question has been validly marked a "duplicate" and all your "hostname" stuff is completely wrong! Basically, what you say IN TEH SAME SENTENCE(!!!) is that you need the local hostname, no the remote hostname. Make up your mind :) – John Smith Dec 13 '13 at 15:49
  • 2
    You're not understanding. The app is sending the local hostname and other server stats as data to a remote source. Let's say my environment has 10 servers. These 10 servers will be sending data to a remote source and I need a way of defining the hostname of the 10 servers so I can differentiate them. I could write this out manually, but need this to be automatic. This has nothing to do with getting the data from a browser. – nocode Dec 13 '13 at 16:00
  • 1
    Very nice - now go ahead and use `os.hostname()` to find the hostname. If that isn't what you want you should think about the presentation of your problem to other people, there's a reason this is the only answer and if the referral to the "duplicate" doesn't help you either you should ask yourself one question: *(I leave this open)* Myself, I have nothing else to add, so no response is necessary. – John Smith Dec 13 '13 at 16:22
  • Excellent! In fact IP (attached to different network interfaces on each machine - for instances "Wi-Fi", "lopback", "LAN", etc.), coming on `os.networkInterfaces()` Object of different properties, is hard to get. But `os.hostname()` or/and `os.platform()` should give you a clue, if in doubt, or want to do some previous tracking, on which server the App is/will be running on. Thank you @John Smith ! – Pedro Ferreira Dec 18 '17 at 18:09
  • 4
    The operating system's host name, as returned by `os.hostname()` has nothing at all to do with the host name portion of an HTTP request. – Vince Mar 23 '18 at 21:48
  • os.hostname() returns localhost not MyCoolMachine .... – RaptoX Jun 19 '20 at 11:43
  • simply put it in .env different for each environment and enjoy the pizza. – Zee Dec 07 '21 at 07:29
21

You can only get the same host name you would get from window.location.hostname if you're running a server with http.createServer. In that case, the host name is one of the properties of the request object:

request.headers.host

I'd love to take credit for this answer, but I'm only here because I didn't know the answer. I found the answer posted at this SO answer.

Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
Vince
  • 3,962
  • 3
  • 33
  • 58
0

Answer by Vince is wrong. request.headers.host is the value passed by the client in the host header.

unless the server is configured with name based virtual hosting, this can be set to anything that looks like an fqdn. in node, you can set it to anything you want or even omit it entirely.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253