Heroku 中的 Node.js 应用程序在 Foreman 上工作,但在部署时不工作?

Posted

技术标签:

【中文标题】Heroku 中的 Node.js 应用程序在 Foreman 上工作,但在部署时不工作?【英文标题】:Node.js app in Heroku Works on Foreman but not When Deployed? 【发布时间】:2012-08-15 10:45:57 【问题描述】:

我成功地让我的应用程序在工头上运行得很好,但是一旦我将它部署到 Heroku,它就会在日志中抛出一个错误:

2012-08-20T03:22:48+00:00 heroku[web.1]: Starting process with command `node index.js`
2012-08-20T03:22:49+00:00 app[web.1]: Server has started.
2012-08-20T03:22:49+00:00 heroku[web.1]: Error R11 (Bad bind) -> Process bound to port 15134, should be 52717 (see environment variable PORT)
2012-08-20T03:22:49+00:00 heroku[web.1]: Stopping process with SIGKILL
2012-08-20T03:22:51+00:00 heroku[web.1]: Process exited with status 137
2012-08-20T03:22:51+00:00 heroku[web.1]: State changed from starting to crashed

我试图更改它绑定到的端口,但每次我这样做都会再次更改端口。我的应用还有其他问题吗?

【问题讨论】:

您是否要对端口进行硬编码? Heroku 每次都希望绑定到不同的端口。你的 Procfile 应该使用 $PORT 变量,如果它还没有的话。 我应该试试。 Node.js 支持吗? Heroku 将设置PORT 环境变量,这是您想要使用的。这可能会对您有所帮助:***.com/questions/7503632/… 不。不起作用,只是说“$PORT”没有定义 我使用的是 Vanilla Node.js,没有 Express 或任何其他框架 【参考方案1】:

在 Heroku 上,您需要使用环境变量中包含的端口: 进程.env.PORT

【讨论】:

以上是关于Heroku 中的 Node.js 应用程序在 Foreman 上工作,但在部署时不工作?的主要内容,如果未能解决你的问题,请参考以下文章