【发布时间】:2017-10-23 12:20:57
【问题描述】:
我在 heroku 上推送了一个相当简单的 node.js express 应用程序,但无法使其工作。它立即崩溃。 heroku tail --logs 给我:
2017-05-23T04:43:08.156660+00:00 app[api]: Scaled to web@1:Free worker@0:Free by user jp@yetie.fr
2017-05-23T04:43:24.388293+00:00 heroku[web.1]: Starting process with command `: node server.js`
2017-05-23T04:43:26.207926+00:00 heroku[web.1]: Process exited with status 0
2017-05-23T04:43:26.220393+00:00 heroku[web.1]: State changed from starting to crashed
2017-05-23T04:43:26.221461+00:00 heroku[web.1]: State changed from crashed to starting
2017-05-23T04:43:43.343050+00:00 heroku[web.1]: Starting process with command `: node server.js`
2017-05-23T04:43:44.751608+00:00 heroku[web.1]: Process exited with status 0
2017-05-23T04:43:44.762870+00:00 heroku[web.1]: State changed from starting to crashed
2017-05-23T04:43:46.400260+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=yetie.herokuapp.com request_id=d7913d1e-64ab-497b-a59d-fda9454d6e69 fwd="81.56.46.53" dyno= connect= service= status=503 bytes= protocol=https
2017-05-23T04:43:46.899099+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=yetie.herokuapp.com request_id=1fdc61fb-eebe-40a2-8b0a-a71b09f7ff10 fwd="81.56.46.53" dyno= connect= service= status=503 bytes= protocol=https
我有一个 Procfile,其中包含:
web : node server.js
worker: node workers/match.js
工人,按预期运行。 server.js 看起来像这样:
const express = require('express');
const app = express();
app.use(express.static(__dirname + '/testheroku'));
app.listen(process.env.PORT || 8080, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
在 /testheroku 目录中有一个“hello world”html 文件。
当我使用heroku local web 在本地运行它时,它可以工作,当我通过使用heroku run bash 然后node server.js 连接到heroku dyno 运行它,然后执行wget http://localhost:xxxx 它正确地获得“hello world”html 文件。
但是当我尝试访问 heroku 在构建应用程序时提供给我的 url 时,我得到了上面的崩溃日志。
Node 和 npm 版本在本地和 heroku 上是相同的:
node --version => v6.10.3
npm --version => 3.10.10
如果需要,我可能会包含 package.json 文件,但它相当长(基于更复杂的 angular2 应用程序)并且它可以在本地和 heroku 上正确编译。当我设法通过连接到 dyno thru bash 来运行 node server.js 时,我怀疑问题应该存在。
我在 stackoveflow 或 elswere 上找到的大多数答案都暗示了端口问题,并使用了 app.listen(process.env.PORT || 8080) 技巧,我这样做了。那么我在这里缺少什么?
【问题讨论】: