Cloud9:找不到开放端口

Nic*_*ick 3 port netstat node.js npm cloud9-ide

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

\n

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

\n
export HOST=$C9_HOSTNAME && export PORT=8080\nnpm run start\n
Run Code Online (Sandbox Code Playgroud)\n

产生npm run start错误:

\n
\n

无法在 appname-username.c9users.io 找到开放端口。

\n

网络错误消息:listen EADDRNOTAVAIL 35.189.252.103

\n
\n

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

\n

有什么想法可以让 Cloud9 本地预览正常运行吗?

\n
\n

根据要求,一些线路来自start.js

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

netstat --listen回复以下信息:

\n
Active Internet connections (only servers)\nProto Recv-Q Send-Q Local Address           Foreign Address         State      \ntcp6       0      0 [::]:ssh                [::]:*                  LISTEN     \nActive UNIX domain sockets (only servers)\nProto RefCnt Flags       Type       State         I-Node   Path\nunix  2      [ ACC ]     STREAM     LISTENING     1533837857 /home/ubuntu/.c9/6614254/collab.sock\nunix  2      [ ACC ]     STREAM     LISTENING     1533835235 /home/ubuntu/.c9/bridge.socket\nunix  2      [ ACC ]     STREAM     LISTENING     1533836998 /tmp/tmux-1000/cloud92.2\n
Run Code Online (Sandbox Code Playgroud)\n

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

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

wil*_*end 5

我对此进行了一些谷歌搜索,我认为问题可能与您设置的主机值有关。根据这个 Cloud9 支持线程,它引用了类似的错误:

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

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

export HOST=0.0.0.0 && export PORT=8080 && npm run start
Run Code Online (Sandbox Code Playgroud)

还刚刚在您链接到的支持页面上找到了这个:

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

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

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

对于“无效的主机头”错误,我认为您设置为 true 是正确的disableHostCheck,但您的 npm script 命令不太可能遵守 from.the CLI 的标志。可能有几种方法可以传递该标志,但最简单的可能是更新代码以在创建开发服务器时设置选项。请记住,这只是一个快速修复,看看我们是否可以让它发挥作用。最好更新一下createDevServerConfig设置选项的函数:

const devServer = new WebpackDevServer(compiler, { ...serverConfig, disableHostCheck: true});
Run Code Online (Sandbox Code Playgroud)

另一个编辑:

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

const devServer = new WebpackDevServer(compiler, { ...serverConfig, public: process.env.PUBLIC_HOST }
Run Code Online (Sandbox Code Playgroud)

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

export HOST=0.0.0.0 && export PORT=8080 && export PUBLIC_HOST=$C9_HOSTNAME:8080 && npm run start
Run Code Online (Sandbox Code Playgroud)

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