在 Node.js Express 中获取当前请求的主机名
所以,我可能在这里遗漏了一些简单的东西,但我似乎找不到一种方法来获取我正在向其发送响应的请求对象所请求的主机名。
是否可以从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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您需要完全限定的域名并且没有 HTTP 请求,在 Linux 上,您可以使用:
If you need a fully qualified domain name and have no HTTP request, on Linux, you could use:
我认为你想要的是识别 跨源请求,你可以使用原始标题。
使用这个你可以获取请求客户端 URL
I think what you want is to identify cross-origin requests, you would instead use the Origin header.
Use this you can get a request Client-Side URL
这是和我一起工作
${req.protocol}://${req.get('host')}
This is work with me
${req.protocol}://${req.get('host')}
您可以从
request
的headers
获取客户端 url,该请求具有属性origin
。所以:You can get the client side url from the
headers
of therequest
, which has a propertyorigin
. So:您可以使用 os 模块:
请参阅 http://nodejs.org/docs/latest /api/os.html#os_os_hostname
注意事项:
如果您可以使用 IP 地址 --
机器可能有多个网卡,除非您指定,否则节点将侦听所有网卡,因此您在请求进入之前不知道请求来自哪个网卡。
主机名是 DNS 问题 --
不要忘记多个 DNS 别名可以指向同一台计算机。
You can use the os Module:
See http://nodejs.org/docs/latest/api/os.html#os_os_hostname
Caveats:
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.
Hostname is a DNS matter --
Don't forget that several DNS aliases can point to the same machine.
如果您谈论的是 HTTP 请求,您可以在以下位置找到请求主机:
但这依赖于传入请求。
更多信息请参见 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:
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.
这是另一个替代方案,
请在 Express 文档 中阅读相关内容。
Here's an alternate
Read about it in the Express Docs.
首先,在提供答案之前,我想先说明一个事实:通过信任标头,您将打开网络钓鱼等安全漏洞的大门。因此,出于重定向目的,在未首先验证 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.