Node.js Cloud9:找不到打开的端口

Node.js Cloud9:找不到打开的端口,node.js,npm,port,cloud9-ide,netstat,Node.js,Npm,Port,Cloud9 Ide,Netstat,我是NodeJS新手,尝试在cloud9ide中建立一个现有的项目(由其他人开发)(我使用的是一个旧的Cloud9帐户;所以不在AWS上运行)。我已经启动git并安装了所有东西。这一切似乎都顺利进行 要在Cloud9之外的本地运行应用程序,您可以使用npm run start启动服务器(我从开发应用程序的人那里知道,这对他很有用)。但是我想在Cloud9中设置它,在Cloud9中有必要首先设置一些变量(如果我不首先定义主机,它会给出错误“无效主机头”)。因此,我使用以下两个命令: export

我是NodeJS新手,尝试在cloud9ide中建立一个现有的项目(由其他人开发)(我使用的是一个旧的Cloud9帐户;所以不在AWS上运行)。我已经启动git并安装了所有东西。这一切似乎都顺利进行

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

export HOST=$C9_HOSTNAME && export PORT=8080
npm run start
npm运行启动
产生错误:

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

网络错误消息:侦听EADDRNOTAVAIL 35.189.252.103

考虑到这一点,我相信我的端口是正确的。我也尝试过值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'
      );
    }
  );
}

我在谷歌上搜索了一下,我想问题可能在于你设置的主机值。根据引用了类似错误:

…您需要改用0.0.0.0,因为c9user.io是代理的公共地址。或者修改/etc/hosts文件。echo“0.0.0.0$C9_主机名”| 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 script命令可能不符合CLI中的标志。可能有几种方法可以传递该标志,但最简单的方法可能是在创建dev服务器时更新代码以设置该选项。请记住,这只是一个快速修复,看看我们是否可以让它工作。最好更新
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 }
然后,您可以像其他变量一样,通过CLI环境传入此值:

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

免责声明:我不认为上面的更改是最好的方法(更新
createDevServerConfig
功能可能会更好,但它们应该可以解决您的问题。可以找到有关
disableHostCheck
选项的更多信息,并且。

如果您找不到如何预览您的跑步应用程序,请尝试在URL中使用端口8080。谢谢@RamThakur,但我恐怕不完全理解您的意思是什么。您的意思是先运行
export HOST=$C9\u主机名:8080
,然后运行
npm run start
?这会产生类似的错误:“在appname username.c9users.io:8080处找不到打开的端口”。我们可以访问node.js代码吗?可能是node.js服务器在代码中指定了一个静态端口,而不是在启动应用程序之前遵循您正在设置的
port
环境变量。@willascend,我已经在帖子中添加了我认为与代码相关的部分。我刚刚扫描了您需要的IP地址ovided with nmap和8080已打开。您确定主机名和应用程序名正确吗?谢谢,我输入
export-HOST=0.0.0&&export-PORT=8080&&npm-run-start
。然后我访问appname-username.c9users.io:8080。这会产生错误“无效主机头”。我读到这是“最常见的原因是应用程序服务器配置错误,导致其拒绝非本地连接。”。我尝试了
npm run start--disable host check
。但这仍然会导致“无效主机头”.Hi@Nick。我编辑了我的答案。如果我提供的内容不起作用,您需要更新
createDevServerConfig
函数。此外,根据
npm run start
脚本的实际内容,可能会更新它并在那里添加CLI选项。如果我将其更改为
const devServer=new WebpackDevServer(编译器,{serverConfig,disableHostCheck:true});
它给出错误“配置对象无效。已使用与API架构不匹配的配置对象初始化了webpack dev服务器。-配置具有未知属性“serverConfig”。”我还尝试了
const devServer=new WebpackDevServer(编译器,serverConfig,{disableHostCheck:true});
但是它似乎没有处理
disableHostCheck
,因为它仍然给出“无效主机头”错误。你好,尼克,你在对象的
serverConfig
标识符前面添加了三个点了吗?我更新了代码,有一个输入错误。请看看现在的代码是否有效。谢谢,太棒了,现在可以了!还有没有办法在不更改代码的情况下禁用主机检查?(这听起来有点不安全,所以如果其他使用该代码的人不需要禁用它以使其在本地运行,我相信他们不希望禁用它。)