Node.js 连接仅适用于 localhost

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14043926/
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 16:50:05  来源:igfitidea点击:

Node.js connect only works on localhost

node.jsconnect

提问by schaz

I've written a small node.js app, using Connect, that serves up a web page, then sends it regular updates. It also accepts and logs user observations to a disk file.

我使用 Connect 编写了一个小型 node.js 应用程序,它提供一个网页,然后定期向它发送更新。它还接受用户观察并将其记录到磁盘文件中。

It works fine as long as I am on localhost, but I can't get other commputers on the same intranet to see it. I am using port 3000, but changing to port 8080 or 80 didn't help.

只要我在本地主机上它就可以正常工作,但是我无法让同一内部网上的其他计算机看到它。我正在使用端口 3000,但更改为端口 8080 或 80 没有帮助。

Here is the code I am using to set up the connection:

这是我用来设置连接的代码:

    
var io = require('socket.io'),
  connect = require('connect');

var app = connect().use(connect.static('public')).listen(3000);
var chat_room = io.listen(app);

As stated above, I've tried changing the port number to 8080 or to 80, and didn't see any difference, so I don't think that it is a firewall problem (but I could be wrong). I've also thought about, after reading similar questions dealing with HTTP, to add 0.0.0.0 to the listen() but it doesn't seem that listen() takes an IP mask parameter.

如上所述,我尝试将端口号更改为 8080 或 80,但没有发现任何区别,因此我认为这不是防火墙问题(但我可能是错的)。在阅读了处理 HTTP 的类似问题后,我也考虑过将 0.0.0.0 添加到 listen() 中,但 listen() 似乎没有采用 IP 掩码参数。

回答by Peter Lyons

Most probably your server socket is bound to the loopback IP address 127.0.0.1instead of the "all IP addresses" symbolic IP 0.0.0.0(note this is NOT a netmask). To confirm this, run sudo netstat -ntlp(If you are on linux) or netstat -an -f inet -p tcp | grep LISTEN(OSX) and check which IP your process is bound to (look for the line with ":3000"). If you see "127.0.0.1", that's the problem. Fix it by passing "0.0.0.0" to the listencall:

很可能您的服务器套接字绑定到环回 IP 地址127.0.0.1而不是“所有 IP 地址”符号 IP 0.0.0.0(注意这不是网络掩码)。要确认这一点,请运行sudo netstat -ntlp(如果您使用的是 linux)或netstat -an -f inet -p tcp | grep LISTEN(OSX)并检查您的进程绑定到哪个 IP(查找带有“:3000”的行)。如果您看到“127.0.0.1”,那就是问题所在。通过将“0.0.0.0”传递给listen调用来修复它:

var app = connect().use(connect.static('public')).listen(3000, "0.0.0.0");

回答by arcseldon

To gain access for other users to your local machine, i usually use ngrok. Ngrok exposes your localhost to the web, and has an NPM wrapper that is simple to install and start:

为了让其他用户访问您的本地机器,我通常使用ngrok。Ngrok 将您的 localhost 暴露给网络,并有一个易于安装和启动的 NPM 包装器:

$ npm install ngrok -g
$ ngrok http 3000

See this example usage:

请参阅此示例用法:

enter image description here

在此处输入图片说明

In the above example, the locally running instance of sails at: localhost:3000is now available on the Internet served at: http://69f8f0ee.ngrok.ioor https://69f8f0ee.ngrok.io

在上面的例子中,本地运行的sails 实例在:localhost:3000现在可以在互联网上获得:http: //69f8f0ee.ngrok.iohttps://69f8f0ee.ngrok.io

回答by Ashish Kaila

Binding to 0.0.0.0 is half the battle. There is an ip firewall (different from the one in system preferences) that blocks TCP ports. Hence port must be unblocked there as well by doing:

绑定到 0.0.0.0 是成功的一半。有一个阻止 TCP 端口的 ip 防火墙(与系统首选项中的防火墙不同)。因此,端口也必须通过以下方式在那里畅通无阻:

sudo ipfw add <PORT NUMBER> allow tcp from any to any

回答by ClusterAtlas

On your app, makes it reachable from any device in the network:

在您的应用上,使其可从网络中的任何设备访问:

app.listen(3000, "0.0.0.0");

For NodeJS in Azure, GCP & AWS

适用于 Azure、GCP 和 AWS 中的 NodeJS

For Azure vm deployed in resource manager, check your virtual network security group and open ports or port ranges to make it reachable, otherwise in your cloud endpoints if vm is deployed in old version of azure.

对于部署在资源管理器中的 Azure vm,请检查您的虚拟网络安全组并打开端口或端口范围以使其可访问,否则如果 vm 部署在旧版本的 azure 中,则在您的云端点中。

Just look for equivalent of it for GCP and AWS

只需为 GCP 和 AWS 寻找等价物

回答by akelec

I have a very simple solution for this problem: process.argvgives you a list of arguments passed to node app. So if you run:

对于这个问题,我有一个非常简单的解决方案:process.argv给你一个传递给 node app 的参数列表。所以如果你运行:

node server.js 0.0.0.0

You'll get:

你会得到:

process.argv[0] //=> "node"
process.argv[1] //=> "server.js"
process.argv[2] //=> "0.0.0.0"

So you can use process.argv[2]to specify that as the IP address you want to listen to:

因此,您可以使用process.argv[2]将其指定为要侦听的 IP 地址:

http.listen(3000, process.argv[2]);

Now, your app is listening to "all" IP addresses, for example http://192.168.1.4:3000/your_app.

现在,您的应用正在侦听“所有”IP 地址,例如http://192.168.1.4:3000/your_app

I hope this will help someone!

我希望这会帮助某人!

回答by soloproper

Fedoraor Centosdistro check your selinux and firewalld in my case firewalld prevented the connection:

FedoraCentos发行版检查您的 selinux 和 firewalld 在我的情况下 firewalld 阻止了连接:

Selinux: $sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   {{checkmode}}
Mode from config file:          {{checkconfig}}
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      30

Firewalld status: $systemctl status firewalld

回答by Meloman

Working for me with this line (simply add --listenwhen running) :

使用这条线对我来说有效(只需在运行时添加--listen):

node server.js -p 3000 -a : --listen 192.168.1.100

Hope it helps...

希望能帮助到你...

回答by Bar Horing

After struggling with this issue for quite some time I managed to solve it by allowing incoming connections on port 8080.

在解决这个问题很长一段时间后,我设法通过允许端口 8080 上的传入连接来解决它。

Since you wrote that the .listen(8080, "0.0.0.0")solution didn't work for you, make sure the connections on port 8080 are allowed through your firewall.

由于您写道该.listen(8080, "0.0.0.0")解决方案对您不起作用,因此请确保允许端口 8080 上的连接通过您的防火墙。

This posthelped me to create a new Inbound rule in windows firewall settings.

这篇文章帮助我在 Windows 防火墙设置中创建了一个新的入站规则。