nocode
nocode

Reputation: 1316

Node JS return hostname

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.

Upvotes: 81

Views: 163564

Answers (4)

sahajraj
sahajraj

Reputation: 26

In Node.js, you can get the hostname of the system using the os module. Here's how you can do it:

const os = require('os');

const hostname = os.hostname();
console.log('Hostname:', hostname);

This code will print out the hostname of the system where the Node.js script is running.

Upvotes: 1

skullnobrains
skullnobrains

Reputation: 11

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.

Upvotes: 1

Vince
Vince

Reputation: 4232

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.

Upvotes: 23

John Smith
John Smith

Reputation: 1824

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 :)

Upvotes: 165

Related Questions