繁体   English   中英

Cloud9:找不到打开的端口

[英]Cloud9: Could not find an open port

我是NodeJS的新手,并试图在Cloud9 IDE中建立一个现有项目(由其他人开发)(我使用的是较旧的Cloud9帐户;因此无法在AWS上运行)。 我已经拉了git并安装了所有东西。 这一切似乎都没有问题。

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

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

npm run start会产生错误:

在appname-username.c9users.io上找不到打开的端口。

网络错误消息:侦听EADDRNOTAVAIL 35.189.252.103

我认为考虑到https://docs.c9.io/docs/run-an-application ,我的端口是正确的。 我也尝试了值8081、8082和$ PORT,但是这些都不起作用。

有什么想法可以使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'
      );
    }
  );
}

我对此进行了一些搜索,我认为问题可能出在您设置的主机值上。 每个引用了类似错误的Cloud9支持线程

...您需要改用0.0.0.0,因为c9user.io是代理的公共地址。 或修改/ etc / hosts文件。 回声“ 0.0.0.0 $ C9_HOSTNAME” | sudo tee -a / etc / hosts

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

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

也可以在您链接到的支持页面上找到它:

如果要开发服务器应用程序,请注意,您需要收听0.0.0.0($ IP)和8080($ PORT)。 监听此端口将使您的应用程序可以在http://-.c9users.io上查看

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

编辑(响应于返回其他错误):

对于“无效的主机头”错误,我认为您在将disableHostCheck设置为true的正确轨道上,但是您的npm脚本命令不太可能坚持使用CLI的标志。 可能有几种方法可以传递该标志,但最简单的方法可能是在创建开发服务器时更新代码以设置选项。 请记住,这只是一个快速解决方案,以了解我们是否可以使其正常工作。 最好更新createDevServerConfig函数以设置选项:

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

另一个编辑:

disableHostCheck选项不安全,可能会使您面临漏洞。 在本地测试时,它被认为是一种快速修复,仅应在封闭的网络中使用。 要在暴露的环境中修复“无效的主机头”,请使用public选项,其中public是您的DNS主机名或公共IP地址:

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

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

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

免责声明:我不认为上述更改是执行此操作的最佳方法(更新createDevServerConfig函数可能会更好,但它们应该可以解决您的问题。有关disableHostCheck选项的更多信息,请参见此处此处这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM