在 node.js Express 中获取当前请求的主机名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7507015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:36:12  来源:igfitidea点击:

Get hostname of current request in node.js Express

node.jsexpresshostname

提问by Jesse

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?

是否可以从 node.js 中找出用户当前正在访问的主机名?

回答by stephenbez

You can use the os Module:

您可以使用 os 模块:

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

See http://nodejs.org/docs/latest/api/os.html#os_os_hostname

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.

  1. 如果您可以使用 IP 地址 - 机器可能有多个网卡,除非您指定它,否则节点将侦听所有网卡,因此您在请求进入之前不知道请求来自哪个网卡。

  2. 主机名是 DNS 问题——不要忘记多个 DNS 别名可以指向同一台机器。

回答by cjohn

If you're talking about an HTTP request, you can find the request host in:

如果您在谈论 HTTP 请求,您可以在以下位置找到请求主机:

request.headers.host

But that relies on an incoming request.

但这依赖于传入的请求。

More at http://nodejs.org/docs/v0.4.12/api/http.html#http.ServerRequest

更多信息请访问http://nodejs.org/docs/v0.4.12/api/http.html#http.ServerRequest

If you're looking for machine/native information, try the process object.

如果您正在寻找机器/本机信息,请尝试使用 process 对象。

回答by Eddie

Here's an alternate

这是一个替代方案

req.hostname

Read about it in the Express Docs.

Express Docs 中阅读它。

回答by dricket

If you need a fully qualified domain name and have no HTTP request, on Linux, you could use:

如果您需要一个完全限定的域名并且没有 HTTP 请求,在 Linux 上,您可以使用:

var child_process = require("child_process");

child_process.exec("hostname -f", function(err, stdout, stderr) {
  var hostname = stdout.trim();
});

回答by arboreal84

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.

首先,在提供答案之前,我想先说明一个事实,即通过信任标头,您正在打开网络钓鱼等安全漏洞的大门。因此,出于重定向目的,请不要在未首先验证 URL 已授权的情况下使用标头中的值。

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.

然后,您的操作系统主机名可能不一定与 DNS 匹配。事实上,一个 IP 可能有多个 DNS 名称。因此,对于 HTTP 目的,不能保证在操作系统配置中分配给您的机器的主机名可用。

The best choice I can think of is to obtain your HTTP listener public IP and resolve its name via DNS. See the dns.reversemethod for more info. But then, again, note that an IP might have multiple names associated with it.

我能想到的最佳选择是获取您的 HTTP 侦听器公共 IP 并通过 DNS 解析其名称。有关dns.reverse更多信息,请参阅方法。但是,再次请注意,一个 IP 可能有多个与之关联的名称。