【问题标题】:Error on setting up my own IP through http.createServer.listen()通过 http.createServer.listen() 设置我自己的 IP 时出错
【发布时间】:2020-11-07 02:24:45
【问题描述】:

我使用 aws cloud9 作为后端,想通过自己的公共 IP 访问 index.html。但是,出现错误,无法成功运行。这里有什么问题?

var http = require('http');
var fs = require('fs');
var url = require('url');

http.createServer( function (request, response) {
  var pathname = url.parse(request.url).pathname;
  console.log("Trying to find '" + pathname.substr(1) + "'...");

  fs.readFile(pathname.substr(1), function (err, data) {
    if (err) {
      response.writeHead(404, {'Content-Type': 'text/html'});
      response.write("ERROR: Cannot find '" + pathname.substr(1) + "'.");
      console.log("ERROR: Cannot find '" + pathname.substr(1) + "'.");
    } else {
      console.log("Found '" + pathname.substr(1) + "'.");
      response.writeHead(200, {'Content-Type': 'text/html'});
      response.write(data.toString());
    }
    response.end();
  });
}).listen(8080, '54.25x.xxx.xxx'); // Or 8081 or 8082 instead of 8080. Or '127.0.0.1' instead of 'localhost'.

如果我输入 .listen(8080, 'localhost'),代码可以运行并创建服务器 但是,如果我将其更改为 IPv4 公共 IP,则无法运行。

错误如下:

Debugger listening on ws://127.0.0.1:15454/*********


For help, see: https://nodejs.org/en/docs/inspector

Debugger attached.
events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRNOTAVAIL: address not available 54.25*.***.***:8080
    at Server.setupListenHandle [as _listen2] (net.js:1263:19)
    at listenInCluster (net.js:1328:12)
    at doListen (net.js:1461:7)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Emitted 'error' event at:
    at emitErrorNT (net.js:1307:8)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    [... lines matching original stack trace ...]
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Waiting for the debugger to disconnect...

【问题讨论】:

    标签: node.js linux amazon-web-services http server


    【解决方案1】:

    我对“使用 aws cloud9 作为后端”的说法感到困惑(这不是 Cloud9 的意思),但我不会解释概念并坚持你的直接问题。

    假设您关注AWS documentation on the matter,因为您已经知道您的公共 IP,有两件重要的事情不要忘记:

    1. 根据Step 2,您必须将安全组入站规则添加到 Cloud9 环境 EC2,用于应用程序的侦听端口(例如 8080)和公共 Internet 访问(或只是您自己的本地计算机公共 IP,如您所愿)。与步骤 3 中描述的已允许所有入站流量的子网 NACL 设置相反,EC2 安全组(由 Cloud9 预定义)仅允许 SSH。
    2. 如果您按照文档进行操作,则可能是您的主要问题 - 我不知道 AWS 文档为什么要您 use EC2 public IP。可怜的 EC2 盒子甚至没有与该 IP 的接口 - 因此EADDRNOTAVAIL。通过在控制台中输入 ifconfig 来检查自己。所以这里有两个解决方案:
      1. 将您在ifconfig 中看到的IP 与eth0 一起使用
      2. 使用0.0.0.0,意思是“监听任何可用的网络地址”。这也会在您不想要的适配器上监听(例如运行 Cloud9 主机的 Docker 适配器),这对您来说可能是个问题,但更有可能不是。

    无论如何,这实际上只是一个临时解决方案。您可能随时丢失特定的公共 IP。如果您想要更持久的设置,请将弹性 IP 附加到 Cloud9 EC2 实例。

    【讨论】:

    • 好的,谢谢。我使用 0.0.0.0,然后它可以工作,但在 AWS 文档中,它说“在您的代码中,从使用 IP 127.0.0.1、localhost 或 0.0.0.0 切换到使用您在前面的步骤中指定的 IP 地址或地址在本节中。“我只是按照这个并将 ip 从 localhost 更改为我自己的
    • 如你所说,我可以使用弹性IP来处理丢失特定公共IP的问题吗?
    • 是的,这正是我在第 2 章中提到的 AWS 令人困惑的部分。它根本不像记录的那样工作。 Re Elastic IP - 你绝对可以,我将它添加到我的答案中。仅当您完成 EC2/Cloud9 后,不要忘记发布 EIP。 AWS 对已分配但未发布的 EIP 每小时收费。
    猜你喜欢
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多