Node.js 连接仅适用于本地主机 [英] Node.js connect only works on localhost

查看:40
本文介绍了Node.js 连接仅适用于本地主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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);

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

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() method but it doesn't seem that listen() takes an IP mask parameter.

推荐答案

很可能您的服务器套接字绑定到环回 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 调用来修复它:

Most probably your server socket is bound to the loopback IP address 127.0.0.1 instead 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 listen call:

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

这篇关于Node.js 连接仅适用于本地主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆