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

发布于 2024-12-06 03:25:04 字数 101 浏览 1 评论 0原文

所以,我可能在这里遗漏了一些简单的东西,但我似乎找不到一种方法来获取我正在向其发送响应的请求对象所请求的主机名。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

来世叙缘 2024-12-13 03:25:05

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

var child_process = require("child_process");

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

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

var child_process = require("child_process");

child_process.exec("hostname -f", function(err, stdout, stderr) {
  var hostname = stdout.trim();
});
伪心 2024-12-13 03:25:05

我认为你想要的是识别 跨源请求,你可以使用原始标题。

const origin = req.get('origin');

使用这个你可以获取请求客户端 URL

I think what you want is to identify cross-origin requests, you would instead use the Origin header.

const origin = req.get('origin');

Use this you can get a request Client-Side URL

愁杀 2024-12-13 03:25:05

这是和我一起工作
${req.protocol}://${req.get('host')}

This is work with me
${req.protocol}://${req.get('host')}

初雪 2024-12-13 03:25:05

您可以从 requestheaders 获取客户端 url,该请求具有属性 origin。所以:

request.headers.origin

You can get the client side url from the headers of the request, which has a property origin. So:

request.headers.origin
沫尐诺 2024-12-13 03:25:04

您可以使用 os 模块:

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

请参阅 http://nodejs.org/docs/latest /api/os.html#os_os_hostname

注意事项:

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

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

You can use the os Module:

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

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

べ繥欢鉨o。 2024-12-13 03:25:04

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

request.headers.host

但这依赖于传入请求。

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

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

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

request.headers.host

But that relies on an incoming request.

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

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

于我来说 2024-12-13 03:25:04

这是另一个替代方案,

req.hostname

请在 Express 文档 中阅读相关内容。

Here's an alternate

req.hostname

Read about it in the Express Docs.

長街聽風 2024-12-13 03:25:04

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

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

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

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.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文