Node.js:无法从同一网络上的不同设备访问服务器

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

Node.js: Cannot access server from different device on the same network

node.js

提问by AbsoluteZero2A03

NOTE: There are some others who have had similar problems, but those were solved by fixing small tidbits in the code involving how the server was listening (in examples that I've seen they put '127.0.0.1' as an argument in http.createServer(...).listen(). However, I do not have the same issue.

注意:还有一些其他人也遇到了类似的问题,但是通过修复代码中涉及服务器如何侦听的小花絮来解决这些问题(在我见过的示例中,他们将 '127.0.0.1' 作为参数放入http.createServer(...).listen(). 但是,我没有同样的问题。

When I try to connect to my node.js server from a different machine on the same LAN network, Chrome says that it cannot connect.

当我尝试从同一 LAN 网络上的另一台机器连接到我的 node.js 服务器时,Chrome 说它无法连接。

This is testtesttest.js

这是 testtesttest.js

var http = require('http');

http.createServer(function(req,res) {
    res.writeHead(200,{'Content-Type': 'text/plain'});
    res.end('Working');
}).listen(3000);

When I try inputting 192.168.1.73:3000(of course 192.168.1.73 is the ip of the machine that I'm running the server on) into the browser (Chrome, although I've tried other browsers as well and I have similar problems) of my other machine, it gives the error "Oops! Google Chrome could not connect to 192.168.1.73:3000". When I type the same address onto the local machine, it works fine.

当我尝试将192.168.1.73:3000(当然 192.168.1.73 是我正在运行服务器的机器的 ip)输入到我的另一台机器的浏览器(Chrome,虽然我也尝试过其他浏览器并且遇到了类似的问题)时,它给出了错误“糟糕!谷歌浏览器无法连接到 192.168.1.73:3000”。当我在本地机器上输入相同的地址时,它工作正常。

I'm not exactly sure what to do. I honestly hope this is just a stupid mistake on my part (I'm sorry for possibly wasting your time) and not something that I have to go into my router for.

我不确定该怎么做。老实说,我希望这只是我的一个愚蠢的错误(我很抱歉可能浪费了你的时间),而不是我必须进入路由器的东西。

Thanks very much for any help.

非常感谢您的帮助。

回答by user568109

Try changing this

尝试改变这个

.listen(3000);

to this

对此

.listen(3000, "0.0.0.0");

回答by Eric Hannum

Just putting this here in case it saves anyone else. I had this problem for two full days when trying to connect my phone to my local machine... and it was because the wifi on my phone was turned off.

只是把它放在这里,以防它拯救其他人。尝试将手机连接到本地计算机时,我遇到了整整两天的问题……这是因为我手机上的 wifi 已关闭。

回答by reverie_ss

I too had this problem. I solved it by allowing "node.js" in the network group.

我也有这个问题。我通过在网络组中允许“node.js”来解决它。


Solution : Allowing "node.js" through the private network windows firewall
a. Go to Control Panel
b. Go to Windows Firewall
c. Click on the Allow an app or feature through windows firewallin the left sidebar
Click on the *Allow an app or feature through windows firewall* in the left sidebar
d. Search for "Node.js : Server Side JavaScript" and make sure both public and private column box is marked tick for "NodeJS" enter image description heree. Click "OK" and you are done


解决方案:允许“node.js的”通过专用网络windows防火墙
一个。转到控制面板
b. 转到 Windows 防火墙
c. 单击左侧边栏中的允许应用程序或功能通过 windows 防火墙d。搜索“Node.js : Server Side JavaScript”并确保公共和私有列框都标记为“NodeJS” e。点击“确定”,你就完成了
Click on the *Allow an app or feature through windows firewall* in the left sidebar
enter image description here

回答by sumitsabhnani

You have to open 3000 port so that it can be accessed from remote machines. You can do that in iptables file. Use below command to open file

您必须打开 3000 端口,以便可以从远程机器访问它。你可以在 iptables 文件中做到这一点。使用以下命令打开文件

vi /etc/sysconfig/iptables

now add below line before the reject lines in that file

现在在该文件中的拒绝行之前添加以下行

-A INPUT -p tcp -m tcp --dport 3000 -j ACCEPT

Now restart iptables service using below command

现在使用以下命令重新启动 iptables 服务

service iptables restart

Now you restart your server and try again. It should work..

现在您重新启动服务器并重试。它应该工作..

回答by Umamaheswaran

Below is the step i followed which Worked

以下是我遵循的步骤

My server code

我的服务器代码

var http=require('http');
http.createServer(function(request,response){
response.writeHead(200,{'Content-Type':'text/plain'});
response.end('Im Node.js.!\n');
console.log('Handled request');
}).listen(8080, "0.0.0.0");;
console.log('Server running a http://localhost:8080/');

Added inbound Rules.

添加了入站规则。

  1. Created a udp inbound rule(since i could'nt find any http protocol).
  2. Once created go to properties for the created rule.
  3. Choose Protocols and Properties tab.
  4. Choose any in Port Type. Click apply and Ok. Now i tried from other machine it worked!!!
  1. 创建了一个 udp 入站规则(因为我找不到任何 http 协议)。
  2. 创建后转到已创建规则的属性。
  3. 选择协议和属性选项卡。
  4. 在端口类型中选择任何。单击应用和确定。现在我从其他机器上试过它工作了!!!

I think you need to set Port Type to any to make it work.

我认为您需要将 Port Type 设置为 any 才能使其工作。

Thanks

谢谢

回答by koss

Chances are your firewall settings block incoming request on port 3000. You may want to add firewall inbound rule on this port to allow access to it.

您的防火墙设置可能会阻止端口 3000 上的传入请求。您可能希望在此端口上添加防火墙入站规则以允许对其进行访问。

回答by Bubiec

My problem was that, I have used IP assigned to my ethernet adapterinstead of wifi adapter...

我的问题是,我使用分配给我的以太网适配器的IP而不是wifi 适配器......

And it now works when I connect from any device.

现在当我从任何设备连接时它都可以工作。

回答by Joe Hawkins

For me, the culprit was a VirtualBox Host-only Network interface. The presence of this network interface was causing ipconfigon the server to report 192.168.56.1 instead of the router assigned address of 192.168.1.x. I was accessing the wrong IP all along.

对我来说,罪魁祸首是 VirtualBox Host-only Network 接口。此网络接口的存在导致ipconfig服务器报告 192.168.56.1,而不是路由器分配的地址 192.168.1.x。我一直在访问错误的IP。

To remove the VirtualBox Host-only Network interface:

要删除 VirtualBox Host-only Network 接口:

  1. Open VirtualBox
  2. Go to File>Preference>Network>Host-only Networks
  3. Remove the offending adapter(s)
  1. 打开 VirtualBox
  2. File>Preference>Network>Host-only Networks
  3. 移除有问题的适配器

回答by Joe Hawkins

 http.createServer(function(req,res) {
    res.writeHead(200,{'Content-Type': 'text/plain'});
    res.end('Working');
 }).listen(3000, "192.168.1.36");

I found my IPv4 address on network settings, then specify with listen fun. put also 3000 port. I can reach http://192.168.1.36:3000/via my tablet which is connected same wifi.

我在网络设置中找到了我的 IPv4 地址,然后用听的乐趣指定。也放3000端口。我可以通过连接相同 wifi 的平板电脑访问http://192.168.1.36:3000/

回答by ElTête

You have to run that on the terminal:

你必须在终端上运行它:

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

Explanation here

此处说明