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.
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 :)
( 8 months ago )