19

On the client I can use window.location.hostname to get the hostname. How can I get the same on the server? I need this to work behind an Apache proxy, unfortunately Meteor.absoluteUrl() gives me localhost:3000. I also want it to work for different domains, I want one Meteor app that gives different results for different domains.

This question is somewhat related: Get hostname of current request in node.js Express

Community
  • 1
  • 1
the
  • 21,007
  • 11
  • 68
  • 101

5 Answers5

33

Meteor.absoluteUrl() given that your ROOT_URL env variable is set correctly.

See the following docs: http://docs.meteor.com/#meteor_absoluteurl.

Meteor doesn't know the outside-facing address of the proxy that it's sitting behind, and the (virtual) domain that this proxy was accessed by would have to be forwarded to the Meteor app for it to do what you are asking for. I don't think this is currently supported.

Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
imslavko
  • 6,596
  • 2
  • 34
  • 43
  • This does not address his problem of `Meteor.absoluteURL` doesn't work. If `ROOT_URL` is not set correctly, please explain how to set it correctly. – Nathaniel Ford Jun 18 '14 at 16:53
  • 1
    This is a better solution since it will give you the name of your domain from `ROOT_URL`. The hostname from the other answer would be the 'short form' hostname, e.g on a mac `Meteors-macbook` or on AWS something like `ip-10-100-12-123` as can be set by the server. A Server can not know its own external hostname unless it does a reverse ip lookup, and in this case someone can easily hijack it to another hostname. This answer is the safest and most guaranteed to give you the correct hostname. – Tarang Jun 18 '14 at 17:17
  • @NathanielFord in addition in production environments meteor will not start unless `ROOT_URL` is provided. – Tarang Jun 18 '14 at 17:18
  • Semantically, this answer does not parse in English to a meaningful response to the original post. It was put in the 'Low Quality' queue for possible deletion. I am not making a statement as to the correctness of the answer, only that it is unclear how it is relating unless you are already expert, thereby reducing it's value to future visitors. I recommend cleaning it up and elaborating as to why it is correct. – Nathaniel Ford Jun 18 '14 at 18:16
  • I didn't spend too much time thinking about this question and the answer. If this is answer is bad, please feel free to remove it. – imslavko Jun 18 '14 at 18:56
  • +1 for Akshat, this is the best solution for me too. You can set the ROOT_URL when you launch your app : ROOT_URL=https://dev.yourdomain.com meteor – rebe100x Mar 10 '16 at 10:46
9

According to this you can now get the Host header inside Meteor.publish() and Meteor.methods() calls by accessing:

this.connection.httpHeaders.host

Elsewhere in the application, it's probably difficult to determine the Host header that is being used to connect.

chmac
  • 11,757
  • 3
  • 32
  • 36
  • 1
    `proxy_set_header Host $host;` in nginx will set the `host` header. Diff hosting providers may set it differently. – Michael Cole Jun 15 '17 at 19:17
7

If you want the hostname of the server, as configured in /etc/hostname for example:

With meteorite:

$ mrt add npm

In your server code:

os = Npm.require('os')
hostname = os.hostname()

This has no connection to the Host header provided in the incoming request.

updated answer with some of chmac's words from the comment below

the
  • 21,007
  • 11
  • 68
  • 101
  • Why? Can you give an example, and ideally a better answer? :) – the Oct 29 '14 at 20:43
  • 2
    This gives you the hostname of the server, as configured in `/etc/hostname` for example (exactly what I want!), but it has no connection to the Host header provided in the incoming request. Unfortunately, given Meteor's architecture, the Host header might be difficult to extract. – chmac Dec 10 '14 at 15:04
1

In any server-side meteor file you can add:

if (Meteor.isServer) {

  Meteor.onConnection(function(result){
    var hostname = result.httpHeaders.referer; //This returns http://foo.example.com
  });
}
Werner Bihl
  • 196
  • 2
  • 7
0

You can fetch host as EnvironmentVariable from DDP object in method and publication. Meteor accounts-base package fetch userId via this way.

const currentDomain = function() {
  const currentInvocation = DDP._CurrentMethodInvocation.get() || DDP._CurrentPublicationInvocation.get();
  return currentInvocation.connection.httpHeaders.host;
}