简体   繁体   English

Node JS返回主机名

[英]Node JS return hostname

I'm still learning Node JS and javascript and have an app. 我还在学习Node JS和javascript并有一个应用程序。 I have a configuration file where I need to grab the hostname of the server on Ubuntu 12.04 我有一个配置文件,我需要在Ubuntu 12.04上获取服务器的主机名

I tried something like: 我尝试过类似的东西:

 window.location.hostname

But that did not work. 但那没用。 Sample code below: 示例代码如下:

exports.config = {
    app_name : [ window.location.hostname ]
}

If I use a string, it will load fine, but this is going to be managed through Github and needs to be differentiated when the app loads. 如果我使用一个字符串,它将加载正常,但这将通过Github进行管理,并且需要在应用程序加载时进行区分。

According to the node.js documentation for the "os" module you need to load the "os" module, which has a hostname() function: 根据“os”模块的node.js文档,您需要加载“os”模块,该模块具有hostname()函数:

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

However, that only is the hostname - without the domain name (the FQDN). 但是,这只是主机名 - 没有域名(FQDN)。 There is no easy way to get the FQDN. 没有简单的方法来获取FQDN。 You could use the node.js DNS functions to try to turn the IP address of the server (which you get with os.networkInterfaces() , see doc link above) into a name. 您可以使用node.js DNS函数尝试将服务器的IP地址(使用os.networkInterfaces()获取,请参阅上面的doc链接)到名称中。 The only problem is servers may have different interfaces and names, so you have to make a decision about which one you want. 唯一的问题是服务器可能有不同的接口和名称,因此您必须决定您想要哪一个。

You tried using the window object, but that only exists in the JavaScript runtime environment of browsers. 您尝试使用window对象,但仅存在于浏览器的JavaScript运行时环境中。 Server side JavaScript doesn't have windows, obviously, so there is no window object. 服务器端JavaScript显然没有window ,因此没有window对象。 See this question: "Does node.js have equivalent to window object in browser" . 看到这个问题:“node.js是否与浏览器中的窗口对象相同”

With this information your question is a little strange - in the browser window.location.hostname is the host part of the URL the current page was loaded from. 有了这些信息你的问题有点奇怪 - 在浏览器window.location.hostname是当前页面加载的URL的主机部分。 How do you translate that to a server context? 你如何将其转换为服务器上下文? The code you run on node.js is from that very server, by definition, so you don't actually need this information. 根据定义,您在node.js上运行的代码来自该服务器,因此您实际上并不需要此信息。 You (may) need it in the browser because that information is variable, especially when you run mashups (JS code from various sources) your code may not know where the page it runs on was loaded from. 您(可能)在浏览器中需要它,因为该信息是可变的,尤其是当您运行mashup(来自各种源的JS代码)时,您的代码可能无法知道它运行的页面从何处加载。 On the server you always know it's your local filesystem. 在服务器上,您始终知道它是您的本地文件系统。

By the way, you can always use localhost as the hostname :) 顺便说一下,你总是可以使用localhost作为主机名:)

You can only get the same host name you would get from window.location.hostname if you're running a server with http.createServer . 如果您正在运行带有http.createServer的服务器,则只能window.location.hostname获取相同的主机名。 In that case, the host name is one of the properties of the request object: 在这种情况下,主机名是请求对象的属性之一:

request.headers.host

I'd love to take credit for this answer, but I'm only here because I didn't know the answer. 我很乐意接受这个答案,但我只是因为我不知道答案。 I found the answer posted at this SO answer . 在这个答案中找到了答案

您可以使用

console.log(process.env.host);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM