meteor:如何获取主机名,服务器端
在客户端上,我可以使用window.location.hostname
来获取主机名。 我怎样才能在服务器上得到相同的? 我需要这个工作在Apache代理后面,不幸的是Meteor.absoluteUrl()
给了我localhost:3000
。 我也希望它适用于不同的领域,我想要一个meteor应用程序,为不同的领域提供不同的结果。
这个问题有点相关: 在node.js Express中获取当前请求的主机名
Meteor.absoluteUrl()
因为你的ROOT_URL
envvariables设置正确。
请参阅以下文档: http : //docs.meteor.com/#meteor_absoluteurl 。
meteor并不知道它坐在的代理的外部地址,这个代理被访问的(虚拟)域将不得不被转发到Meteor应用程序,以便它做你所要求的。 我不认为这是目前支持。
据此,您现在可以通过访问Meteor.publish()
和Meteor.methods()
调用中的Host
头部:
this.connection.httpHeaders.host
在应用程序的其他地方,可能很难确定用于连接的Host
头。
如果您需要服务器的/etc/hostname
,例如在/etc/hostname
configuration的那样:
用陨石 :
$ mrt add npm
在你的服务器代码中:
os = Npm.require('os') hostname = os.hostname()
这没有连接到传入请求中提供的主机头。
用下面评论的一些chmac的话更新了答案
在任何服务器端的meteor文件中,你可以添加:
if (Meteor.isServer) { Meteor.onConnection(function(result){ var hostname = result.httpHeaders.referer; //This returns http://foo.example.com }); }