224

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?

9
  • It's in the request headers. Well I thought so but now I don't see it ... hmmmm
    – jcolebrand
    Commented Sep 21, 2011 at 21:53
  • 15
    CJohn found it, it's in request.headers.host - thanks!
    – Jesse
    Commented Sep 22, 2011 at 0:38
  • Thought so ... just wasn't pulling it out of the ole noggin. Glad you got it!
    – jcolebrand
    Commented Sep 22, 2011 at 2:05
  • @Jesse Hey, that is actually not correct. request.headers.host isn't the hostname of the server based on the OS, it's the host header name sent along with the HTTP request. If your server responds to any HTTP request regardless of the host header then that value could be ANYTHING that the client decides to send.
    – Rob Evans
    Commented May 30, 2013 at 12:50
  • 1
    Possible duplicate of How to check the HOST using ExpressJS?
    – niry
    Commented Jan 23, 2018 at 22:57

8 Answers 8

292

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.

11
  • 8
    Although this will work to get the machine's hostname, I can have a machine set up to answer to multiple hosts, so this wouldn't be accurate.
    – Jesse
    Commented Feb 22, 2012 at 18:07
  • 7
    However this one is better - because you can use it in modules that do not run always inside context of HTTP app Commented Nov 21, 2012 at 11:41
  • 8
    And importantly this one answers the question as it was written. I think the question is actually wrong if this is not the answer.
    – Rob Evans
    Commented May 30, 2013 at 13:02
  • 3
    This correlates with the hostname command which should be a valid DNS name but is not required to be. For example, under OS X you'll get names like my-machine.local which is not resolvable with DNS. To figure out the external IP of the machine you'll need to hit a service that performs this function or use the STUN protocol to figure it out.
    – tadman
    Commented Jun 5, 2013 at 14:35
  • 6
    This is always accurate. It returns the machine's hostname, which is what the title of the question asked. (The text of this one asked a different question). The hostname used on an incoming HTTP request is a different matter.
    – Cheeso
    Commented Aug 31, 2013 at 17:40
269

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.

10
  • 6
    Ah, awesome - FWIW, there would be no way to detect the actual host without a request, you could have multiple hosts configured, but this is what I was looking for!
    – Jesse
    Commented Sep 22, 2011 at 0:38
  • 2
    Did the trick, thanks! In Express 4, I had to do this update: req.headers.host
    – Gene Bo
    Commented Jun 24, 2015 at 3:07
  • 7
    Wrong! request.headers.host returns http://127.0.0.1 but not a production server domain name
    – Green
    Commented Jun 28, 2016 at 22:11
  • 14
    req.headers.host is provided by the user. I can craft a request in 1 line of python and send you a request without that field making your code crash
    – arboreal84
    Commented Jul 28, 2016 at 18:31
  • 4
    request.headers.host is now deprecated, instead you can use request.headers.hostname
    – Syam Danda
    Commented Dec 8, 2016 at 9:45
49

Here's an alternate

req.hostname

Read about it in the Express Docs.

4
  • 5
    This is the correct answer, as req.headers.host gives host:port
    – ekerner
    Commented Feb 12, 2019 at 12:38
  • what's the different between req.hostname and req.host Commented Aug 20, 2019 at 16:37
  • 1
    req.host is deprecated. If you use that, express will output a warning says 'deprecated req.host: Use req.hostname instead'
    – bruceczk
    Commented Jan 13, 2020 at 2:51
  • Where is reqsupposed to come from? Does it needs to be imported? Because just adding that line results in the error `Cannot find name 'req'.
    – mrodo
    Commented Mar 14 at 13:25
10

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.

9

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();
});
2
  • 1
    BTW there's also hostname command in Windows (but you invoke it without parameters)
    – jakub.g
    Commented Nov 28, 2016 at 12:37
  • 1
    @jakub.g Yes, BUT, it doesn't return the fully-qualifed host name. Commented May 28, 2021 at 2:46
1

I think what you want is to identify cross-origin requests, you would instead use the Origin header.

const origin = req.get('origin');

Use this you can get a request Client-Side URL

1
  • Where is reqsupposed to come from? Does it needs to be imported? Because just adding that line results in the error Cannot find name 'req'.
    – mrodo
    Commented Mar 14 at 13:25
1

You can simply use below code to get the host.

request.headers.host
1
  • Where is requestsupposed to come from? Does it needs to be imported? Because just adding that line results in the error Cannot find name 'request'.
    – mrodo
    Commented Mar 14 at 13:24
-1

You can get the client side url from the headers of the request, which has a property origin. So:

request.headers.origin
1
  • 1
    it's undefined for me, maybe it's only available in certain types of requests
    – Jean Paul
    Commented Sep 30, 2023 at 15:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.