Node.js连接仅适用于本地主机

编程入门 行业动态 更新时间:2024-10-25 16:30:18
本文介绍了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.

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

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.

这是我用来建立连接的代码:

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() 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连接仅适用于本地主机

本文发布于:2023-11-28 19:45:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643718.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适用于   主机   Node   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!