【问题标题】:Node.JS Not working on the internetNode.JS 无法在互联网上运行
【发布时间】:2011-11-04 16:52:04
【问题描述】:

我在 windows 上为 nodejs 提供了基本的 webserver hello world 应用程序,它可以在 localhost 上运行。但是当我从互联网上测试它时,它无法连接。我在我的网件路由器中设置了端口转发。我是否错过了让我的 nodejs 服务器对外界可见的步骤?

谢谢。

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');

【问题讨论】:

  • 如果您向我们展示您正在使用的代码会有所帮助。
  • 它只是nodejs网站上的hello world web服务器。

标签: porting node.js forward-compatibility


【解决方案1】:

确保您收听0.0.0.0 而不是127.0.0.1

127.0.0.1 是一个仅对您的计算机可见的专用网络。 0.0.0.0监听所有接口,包括私有和公共(尽可能公共,因为它可以在 NAT 后面)。

【讨论】:

  • 酷,你从哪里学来的?甚至不知道 0.0.0.0 是一个有效的 ip。
  • 在 Linuxland 的某个地方捡到它,可能是手动编辑 lighttpd 配置或其他东西。已经有一段时间了。
  • 我也遇到了同样的问题,我确实用了0.0.0.0但是还是不能在外面访问我的服务器。
  • @xybrek:创建一个新问题,不过可能是路由器配置问题。
  • @insta 我刚刚使用 Ubuntu 创建了一个新的 nodejs 服务器,它修复了所有问题。和 iptables
【解决方案2】:

看起来您正在将服务器绑定到 IP 地址127.0.0.1,即 localhost。如果你想在其他地方访问它,你需要将它设置为它的互联网 IP。查看 whatismyip.com 并改用该 IP。

【讨论】:

  • 如果他在 NAT 后面,这将不起作用。他的内部 IP 地址(可能)是 192.168.xx,但他的公共地址将完全不同。
  • 如果您在 NAT 之后该怎么办?
【解决方案3】:

只是为了确保。

您的代码应该像这样运行。

var http = require('http');
const port = 1337;
const host = '0.0.0.0';

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(port, host);
console.log('Server running at http://${host}:${port}');

【讨论】:

    猜你喜欢
    • 2012-05-10
    • 2021-06-26
    • 1970-01-01
    • 2014-11-12
    • 2011-01-12
    • 2013-06-14
    • 2019-03-15
    • 2012-11-10
    • 2017-11-16
    相关资源
    最近更新 更多