简体   繁体   English

Node.JS .listen(port,'hostname')不起作用

[英]Node.JS .listen(port, 'hostname') does not work

at the moment I try to spin up a node.js server and I do want to understand the arguments of server.listen. 目前,我尝试启动一个node.js服务器,但我确实想了解server.listen的参数。

server.listen(port, hostname, backlog, callback);

Question: as far as I understand this the 2. argument of listen should be a hostname. 问题:据我所知,2. listen参数应为主机名。 The result should be, that I am able to reach the server over "hostname:7000" but the result is, that the script is crashing. 结果应该是,我可以通过“主机名:7000”访问服务器,但是结果是,脚本崩溃了。 Without "hostname" everything works fine. 没有“主机名”,一切正常。 Whats the problem here? 这里有什么问题? What is the usage of "hostname"? “主机名”的用法是什么?

const server = http.createServer(function (req, res) {    
console.log(req); 

}); 

server.listen(7000, "bla");

Browser: 浏览器:

bla:7000

doesn't work. 不起作用。

Error: 错误:

Error: listen EADDRNOTAVAIL 22.0.0.0:7000
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at Server._listen2 (net.js:1246:19)
at listen (net.js:1295:10)
at net.js:1405:9
at _combinedTickCallback (internal/process/next_tick.js:77:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
at Module.runMain (module.js:606:11)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)

The hostname argument is used in situations where a server has more than one network interface, and you only want the server to listen on one of those interfaces (as opposed to the default, which is to listen to all interfaces). hostname参数用于服务器具有多个网络接口的情况,并且您只希望服务器在这些接口之一上侦听(与之相反,默认侦听所有接口)。

For instance, if you want the server to be only accessible by clients running on the server itself, you make it listen on the loopback network interface, which has IP-address "127.0.0.1" or hostname "localhost": 例如,如果您希望服务器只能由运行在服务器本身上的客户端访问,则可以使其在回送网络接口上侦听,该接口的IP地址为“ 127.0.0.1”或主机名为“ localhost”:

server.listen(7000, "localhost")
server.listen(7000, "127.0.0.1")

It doesn't mean that you can just enter any hostname and magically get the ability to access the server through that hostname, that's not how it works or what it is intended for. 这并不意味着您只能输入任何主机名,就可以通过该主机名神奇地获得访问服务器的能力,这不是它的工作原理或用途。

You are not allowed to give any random string as host argument. 不允许提供任何随机字符串作为主机参数。 The server tries to bind itself to the provided hostname. 服务器尝试将其自身绑定到提供的主机名。 So your hostname should either be your ip or a reachable hostname from the dns. 因此,您的主机名应该是您的ip或从dns可以访问的主机名。

If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise. 如果省略host,则当IPv6可用时,服务器将接受未指定的IPv6地址(::)上的连接,否则将接受未指定的IPv4地址(0.0.0.0)。

I had a similar problem, because the router or the proxy modified the hostname of the request... The result was 2 differents names for intranet and extranet clients. 我遇到了类似的问题,因为路由器或代理修改了请求的主机名...结果是Intranet和Extranet客户端的2个不同名称。

My solution was to set blank the hostname 我的解决方案是将主机名设置为空白

server.listen(7000, "")

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

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