Meteor:如何获取主机名,服务器端

时间:2022-05-08 15:23:40

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.

在客户端上,我可以使用window.location.hostname来获取主机名。如何在服务器上获得相同的内容?我需要这个在Apache代理后面工作,不幸的是Meteor.absoluteUrl()给了我localhost:3000。我也希望它适用于不同的域,我想要一个Meteor应用程序,为不同的域提供不同的结果。

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

这个问题有点相关:在node.js Express中获取当前请求的主机名

5 个解决方案

#1


30  

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

Meteor.absoluteUrl(),因为您的ROOT_URL env变量设置正确。

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

请参阅以下文档: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.

Meteor不知道它所在的代理的面向外部的地址,并且访问该代理的(虚拟)域必须转发到Meteor应用程序,以便它能够满足您的要求。我认为目前不支持此功能。

#2


9  

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

根据这个,您现在可以通过访问以下内容获取Meteor.publish()和Meteor.methods()调用中的Host头:

this.connection.httpHeaders.host

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

在应用程序的其他地方,可能很难确定用于连接的Host头。

#3


7  

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

如果您想要服务器的主机名,例如在/ etc / hostname中配置:

With meteorite:

用陨石:

$ mrt add npm

$ 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.

这与传入请求中提供的Host头没有任何关联。

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

用以下评论中的chmac的一些单词更新答案

#4


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
  });
}

#5


0  

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

您可以在方法和发布中从DDP对象获取HostVariable作为主机。 Meteor账户基础包通过这种方式获取userId。

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

#1


30  

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

Meteor.absoluteUrl(),因为您的ROOT_URL env变量设置正确。

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

请参阅以下文档: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.

Meteor不知道它所在的代理的面向外部的地址,并且访问该代理的(虚拟)域必须转发到Meteor应用程序,以便它能够满足您的要求。我认为目前不支持此功能。

#2


9  

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

根据这个,您现在可以通过访问以下内容获取Meteor.publish()和Meteor.methods()调用中的Host头:

this.connection.httpHeaders.host

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

在应用程序的其他地方,可能很难确定用于连接的Host头。

#3


7  

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

如果您想要服务器的主机名,例如在/ etc / hostname中配置:

With meteorite:

用陨石:

$ mrt add npm

$ 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.

这与传入请求中提供的Host头没有任何关联。

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

用以下评论中的chmac的一些单词更新答案

#4


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
  });
}

#5


0  

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

您可以在方法和发布中从DDP对象获取HostVariable作为主机。 Meteor账户基础包通过这种方式获取userId。

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