Cloud9:找不到开放端口

Cloud9: Could not find an open port

我是 NodeJS 的新手,正在尝试在 Cloud9 IDE 中设置一个现有项目(由其他人开发)(我使用的是旧的 Cloud9 帐户;所以不是 运行ning在 AWS 上)。我已经拉 git 并安装了所有东西。这一切似乎都没有问题。

要在 Cloud9 之外的本地 运行 应用程序,您可以使用 npm run start 启动服务器(我从开发该应用程序的人那里得知,这对他有用)。但是我想在Cloud9中设置它,在Cloud9中需要先设置一些变量(如果我不先定义主机,它会给出错误“Invalid Host header”)。因此,我使用以下两个命令:

export HOST=$C9_HOSTNAME && export PORT=8080
npm run start

npm run start 产生错误:

Could not find an open port at appname-username.c9users.io.

Network error message: listen EADDRNOTAVAIL 35.189.252.103

考虑到 https://docs.c9.io/docs/run-an-application,我相信我的端口是正确的。我还尝试了值 8081、8082 和 $PORT,但这些值 none 有效。

关于如何使 Cloud9 本地预览工作的任何想法?


根据请求 start.js 中的一些行:

const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
const HOST = process.env.HOST || '0.0.0.0';
console.log(`1. The host is ${HOST} on port ${DEFAULT_PORT}`);  //ADDED

choosePort(HOST, DEFAULT_PORT)
  .then(port => {
    console.log(`2. The host is ${HOST} on port ${DEFAULT_PORT}`);  //ADDED
    if (port == null) {
      // We have not found a port.
      return;
    }
    const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
    const appName = require(paths.appPackageJson).name;
    const urls = prepareUrls(protocol, HOST, port);
    // Create a webpack compiler that is configured with custom messages.
    const compiler = createCompiler(webpack, config, appName, urls, useYarn);
    // Load proxy config
    const proxySetting = require(paths.appPackageJson).proxy;
    const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
    // Serve webpack assets generated by the compiler over a web sever.
    const serverConfig = createDevServerConfig(
      proxyConfig,
      urls.lanUrlForConfig
    );
    const devServer = new WebpackDevServer(compiler, serverConfig);
    // Launch WebpackDevServer.
    devServer.listen(port, HOST, err => {
      if (err) {
        return console.log(err);
      }
      if (isInteractive) {
        clearConsole();
      }
      console.log(chalk.cyan('Starting the development server...\n'));
      openBrowser(urls.localUrlForBrowser);
    });

  })
  .catch(err => {
    if (err && err.message) {
      console.log(err.message);
    }
    process.exit(1);
  });

netstat --listen 响应以下信息:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN     
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     1533837857 /home/ubuntu/.c9/6614254/collab.sock
unix  2      [ ACC ]     STREAM     LISTENING     1533835235 /home/ubuntu/.c9/bridge.socket
unix  2      [ ACC ]     STREAM     LISTENING     1533836998 /tmp/tmux-1000/cloud92.2

函数choosePort是节点模块“react-dev-utils”的一部分,内容如下:

function choosePort(host, defaultPort) {
  return detect(defaultPort, host).then(
    port => new Promise(resolve => {
      if (port === defaultPort) {
        return resolve(port);
      }
      if (isInteractive) {
        clearConsole();
        const existingProcess = getProcessForPort(defaultPort);
        const question = {
          type: 'confirm',
          name: 'shouldChangePort',
          message: chalk.yellow(
            `Something is already running on port ${defaultPort}.` +
              `${existingProcess ? ` Probably:\n  ${existingProcess}` : ''}`
          ) + '\n\nWould you like to run the app on another port instead?',
          default: true,
        };
        inquirer.prompt(question).then(answer => {
          if (answer.shouldChangePort) {
            resolve(port);
          } else {
            resolve(null);
          }
        });
      } else {
        console.log(
          chalk.red(`Something is already running on port ${defaultPort}.`)
        );
        resolve(null);
      }
    }),
    err => {
      throw new Error(
        chalk.red(`Could not find an open port at ${chalk.bold(host)}.`) +
          '\n' +
          ('Network error message: ' + err.message || err) +
          '\n'
      );
    }
  );
}

我对此进行了一些谷歌搜索,我认为问题可能出在您设置的主机值上。根据 this Cloud9 support thread 引用类似的错误:

...You need to use 0.0.0.0 instead since c9user.io is the public address of the proxy. Or modify your /etc/hosts file. echo "0.0.0.0 $C9_HOSTNAME" | sudo tee -a /etc/hosts

因此,尝试将主机设置为 0.0.0.0 而不是 public 主机名:

export HOST=0.0.0.0 && export PORT=8080 && npm run start

还在您链接到的 support page 上发现了这个:

If you're developing a server application, please note that you need to listen to 0.0.0.0 ($IP) and 8080 ($PORT). Listening to this port will enable your app to be viewable at http://-.c9users.io

监听 0.0.0.0 应该可以解决问题。

编辑(响应返回的附加错误):

对于 "Invalid host header" 错误,我认为您将 disableHostCheck 设置为 true 是正确的,但是您的 npm 脚本命令不太可能遵守标志 from.the命令行界面。可能有几种方法可以通过该标志,但最简单的可能是更新您的代码以在创建开发服务器时设置该选项。请记住,这只是一个快速修复,看看我们是否可以让它工作。最好更新 createDevServerConfig 函数来设置选项:

const devServer = new WebpackDevServer(compiler, { ...serverConfig, disableHostCheck: true});

另一个编辑:

disableHostCheck 选项不安全,可能会使您容易受到漏洞攻击。在本地测试时,它被认为是一种快速修复,并且只应在封闭网络中使用。要在暴露的环境中修复 "Invalid host header",请使用 public 选项,其中 public 是您的 DNS 主机名或 public IP 地址:

const devServer = new WebpackDevServer(compiler, { ...serverConfig, public: process.env.PUBLIC_HOST }

然后您可以像其他变量一样通过 CLI 环境传递此值:

export HOST=0.0.0.0 && export PORT=8080 && export PUBLIC_HOST=$C9_HOSTNAME:8080 && npm run start

免责声明:我不认为上述更改是执行此操作的最佳方法(更新 createDevServerConfig 功能可能会更好,但它们应该可以解决您的问题。更多信息关于disableHostCheck 选项可以在 here, here, and here.

中找到