【问题标题】:How can I access Heroku's assigned port with sirv?如何使用 sirv 访问 Heroku 分配的端口?
【发布时间】:2020-03-26 19:53:59
【问题描述】:

我收到 Heroku H20 错误(应用程序启动超时),然后是 H10 错误(Web 进程未能在启动后 60 秒内绑定到 $PORT)

我的入门 svelte 项目正在 package.json 中使用 sirv:

{
  "name": "clicker",
  "version": "0.0.1",
  "scripts": {
    "build": "rollup -c",
    "dev": "rollup -c -w",
    "start": "sirv public"
  },
  "engines": {
    "node": "10.16.0",
    "npm": "6.13.1"
  },
  "devDependencies": {
    "rollup": "^1.12.0",
    "rollup-plugin-commonjs": "^10.0.0",
    "rollup-plugin-livereload": "^1.0.0",
    "rollup-plugin-node-resolve": "^5.2.0",
    "rollup-plugin-svelte": "^5.0.3",
    "rollup-plugin-terser": "^5.1.2",
    "svelte": "^3.0.0"
  },
  "dependencies": {
    "sirv-cli": "^0.4.4"
  }
}

我似乎无法弄清楚如何通过 Heroku 提供的 $PORT 环境变量。我试过了

"start": "sirv public --port $PORT"

The closest solution 我发现是指 Heroku 动态地为您的应用程序分配端口的事实,但我发现的所有解决方案都是指在节点中使用 process.env.port,但我似乎无法弄清楚如何在我的 package.json 文件中与 sirv 一起使用。它基本上不是在命令行上运行'sirv public'吗?所以我不能只在那里使用 process.env.port 因为那是为了访问代码中的端口变量......

Heroku 日志

2019-12-02T07:54:26.210322+00:00 app[web.1]: Your application is ready~! �????
2019-12-02T07:54:26.210323+00:00 app[web.1]:
2019-12-02T07:54:26.210377+00:00 app[web.1]:   - Local:      http://localhost:19516
2019-12-02T07:54:26.210621+00:00 app[web.1]:
2019-12-02T07:54:26.210624+00:00 app[web.1]:  LOGS
2019-12-02T07:54:26.210625+00:00 app[web.1]:
2019-12-02T07:55:08.914191+00:00 heroku[router]: at=error code=H20 desc="App boot timeout"
2019-12-02T07:55:24.799059+00:00 heroku[web.1]: State changed from starting to crashed
2019-12-02T07:55:24.713988+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-12-02T07:55:24.714032+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-12-02T07:55:24.782218+00:00 heroku[web.1]: Process exited with status 137
2019-12-02T08:02:13.343213+00:00 heroku[router]: at=error code=H10 desc="App crashed"

有什么想法吗?

【问题讨论】:

    标签: node.js heroku svelte


    【解决方案1】:

    创建一个.env 文件(添加到.gitignore),内容如下:

    HOST=localhost
    PORT=5000
    

    package.json启动命令:

    {
      "start": "sirv public --host $HOST --port $PORT"
    }
    

    在本地运行以进行开发时执行:

    heroku local
    

    这将从.env获取您的环境变量

    在生产中,Heroku 将提供 $HOST$PORT

    $HOST 可以根据需要替换为硬编码的0.0.0.0

    Why is my Node.js app crashing with an R10 error?

    【讨论】:

      【解决方案2】:
      By default, the server will only respond to requests from localhost. To allow connections from other computers, edit the sirv commands in package.json to include the option --host 0.0.0.0.
      

      我将它添加到我的 package.json 中,它已成功完全使用 heroku!

      【讨论】:

        猜你喜欢
        • 2020-03-16
        • 2020-02-10
        • 2016-10-04
        • 2017-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-13
        相关资源
        最近更新 更多