Get hostname of current request in node.js Express

node.jsExpressHostname

node.js Problem Overview


So, I may be missing something simple here, but I can't seem to find a way to get the hostname that a request object I'm sending a response to was requested from.

Is it possible to figure out what hostname the user is currently visiting from node.js?

node.js Solutions


Solution 1 - node.js

You can use the os Module:

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

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

Caveats:

  1. if you can work with the IP address -- Machines may have several Network Cards and unless you specify it node will listen on all of them, so you don't know on which NIC the request came in, before it comes in.

  2. Hostname is a DNS matter -- Don't forget that several DNS aliases can point to the same machine.

Solution 2 - node.js

If you're talking about an HTTP request, you can find the request host in:

request.headers.host

But that relies on an incoming request.

More at http://nodejs.org/docs/v0.4.12/api/http.html#http.ServerRequest

If you're looking for machine/native information, try the process object.

Solution 3 - node.js

Here's an alternate

req.hostname

Read about it in the Express Docs.

Solution 4 - node.js

If you need a fully qualified domain name and have no HTTP request, on Linux, you could use:

var child_process = require("child_process");

child_process.exec("hostname -f", function(err, stdout, stderr) {
  var hostname = stdout.trim();
});

Solution 5 - node.js

First of all, before providing an answer I would like to be upfront about the fact that by trusting headers you are opening the door to security vulnerabilities such as phishing. So for redirection purposes, don't use values from headers without first validating the URL is authorized.

Then, your operating system hostname might not necessarily match the DNS one. In fact, one IP might have more than one DNS name. So for HTTP purposes there is no guarantee that the hostname assigned to your machine in your operating system configuration is useable.

The best choice I can think of is to obtain your HTTP listener public IP and resolve its name via DNS. See the dns.reverse method for more info. But then, again, note that an IP might have multiple names associated with it.

Solution 6 - node.js

Either pass & get it from the frontend header OR simply put it in .env where .env file is ignored by .gitignore so for each server environment you'll be having different .env with host string of that server in it

.env code

HOSTNAME=example.com

file where you want hotname.

const dotenv = require("dotenv");
dotenv.config();

console.log('hostname: '+process.env.HOSTNAME)

OUTPUT:

hostname: example.com

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJesseView Question on Stackoverflow
Solution 1 - node.jsstephenbezView Answer on Stackoverflow
Solution 2 - node.jscjohnView Answer on Stackoverflow
Solution 3 - node.jsEddieView Answer on Stackoverflow
Solution 4 - node.jsdricketView Answer on Stackoverflow
Solution 5 - node.jsarboreal84View Answer on Stackoverflow
Solution 6 - node.jsMohammad ZeeshanView Answer on Stackoverflow