我有一个在 Heroku 上托管的 Express 应用程序。在本地它运行得很好,但在 Heroku 上总是崩溃。每次我访问它时,我都会收到“应用程序错误”页面。
我仔细检查了所有环境变量,它们看起来没问题。
Heroku 日志并没有提供太多帮助:
2017-04-16T23:02:49.001768+00:00 heroku[web.1]: State changed from crashed
to starting
2017-04-16T23:02:52.072906+00:00 heroku[web.1]: Starting process with
command `npm run start`
2017-04-16T23:02:56.042611+00:00 app[web.1]:
2017-04-16T23:02:56.042628+00:00 app[web.1]: > <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b6dbcf9bd7c6c6f68698869887" rel="noreferrer noopener nofollow">[email protected]</a> start /app
2017-04-16T23:02:56.042629+00:00 app[web.1]: > node bin/app
2017-04-16T23:02:56.042630+00:00 app[web.1]:
2017-04-16T23:02:57.200912+00:00 heroku[web.1]: State changed from starting
to crashed
2017-04-16T23:02:57.201454+00:00 heroku[web.1]: State changed from crashed
to starting
2017-04-16T23:02:57.192198+00:00 heroku[web.1]: Process exited with status 0
2017-04-16T23:03:00.981528+00:00 heroku[web.1]: Starting process with
command `npm run start`
2017-04-16T23:03:04.312475+00:00 app[web.1]:
2017-04-16T23:03:04.312489+00:00 app[web.1]: > <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c3125713d2c2c1c6c726c726d" rel="noreferrer noopener nofollow">[email protected]</a> start /app
2017-04-16T23:03:04.312490+00:00 app[web.1]: > node bin/app
2017-04-16T23:03:04.312491+00:00 app[web.1]:
2017-04-16T23:03:06.093211+00:00 heroku[web.1]: Process exited with status 0
2017-04-16T23:03:06.112842+00:00 heroku[web.1]: State changed from starting
to crashed
然后我尝试手动启动应用程序:
$ heroku run bash
...
$ npm run start
我没有收到任何错误。我正在使用 pm2 进程管理器,因此我检查了它以查看应用程序是否正在运行,看起来确实如此:
$ node_modules/.bin/pm2 list
...
但是当我打开应用程序时,我得到与以前相同的“应用程序错误”页面。
“heroku restart”没有帮助。每次它都会再次崩溃。
我的本地环境和 Heroku 之间的唯一区别(至少据我所知......)是,我在本地有一个文件,从中读取 RSA 私钥,并且在 Heroku 上读取该 key 来自环境。
编辑:根据要求,这是我的 Procfile:
web: npm run start
有什么想法吗?
最佳答案
您的应用程序需要开始监听环境中定义的端口。例如,如果您有一个expressjs应用程序,它应该看起来像这样:
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
app.get('/', (req, res) => res.send("Hello world"))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
如果应用程序未绑定(bind)到正确的端口,heroku 将认为该应用程序不健康并将终止它。
您需要在日志中看到这一行,这表明heroku 认为该应用程序运行正常:
State changed from starting to up
请记住,此端口是 web
进程的特殊要求。您的应用中的其他进程没有这样的要求。
关于node.js - Heroku Node 应用程序不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43443183/