Jea*_*ppe 5 heroku node.js express
我在 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
Run Code Online (Sandbox Code Playgroud)
我有一个 Procfile,其中包含:
web : node server.js
worker: node workers/match.js
Run Code Online (Sandbox Code Playgroud)
工人,按预期运行。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);
});
Run Code Online (Sandbox Code Playgroud)
在 /testheroku 目录中有一个“hello world”html 文件。
当我运行在本地与heroku local web它的工作原理,当我通过连接到Heroku的使用DYNO运行它heroku run bash,然后node server.js,然后做一个wget http://localhost:xxxx得当拿到了“Hello World”的HTML文件。
但是,当我尝试访问 heroku 在构建应用程序时提供给我的 url 时,我得到了上述崩溃日志。
Node 和 npm 版本在本地和 heroku 上是相同的:
node --version => v6.10.3
npm --version => 3.10.10
Run Code Online (Sandbox Code Playgroud)
如果需要,我可能会包含 package.json 文件,但它相当长(基于更复杂的 angular2 应用程序)并且它在本地和 Heroku 上都可以正确编译。当我设法node server.js通过 bash 连接到 dyno来运行时,我怀疑问题是否应该存在。
我在 stackoveflow 或 els 上找到的大多数答案都建议端口问题并使用app.listen(process.env.PORT || 8080)我所做的技巧。那么我在这里错过了什么?
确保您可以真正启动您的应用程序npm start并且它不会退出,就像在 Heroku 上所做的那样。
从日志看来,您的应用程序从状态 0 开始大约 2 秒后退出,如果进程正常退出,通常是这种情况,因为没有更多的事件侦听器,但有时可能是由其他问题引起的,尤其是当您出现异常时被自动捕获,您看不到正确的上下文。
还要确保您的应用程序不依赖于 package.json 中未包含的任何全局安装的依赖项。确保 Heroku 执行所需的所有安装和构建步骤。确保您可以将代码复制到新目录中并使用npm install && npm start.
我会改变这个:
app.listen(process.env.PORT || 8080, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
Run Code Online (Sandbox Code Playgroud)
对此:
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log('Express server listening on port', port)
});
Run Code Online (Sandbox Code Playgroud)
确保它不会因为您要访问的所有变量而崩溃。
如果它仍然不起作用,请查看我在 GitHub 上的演示:
它还使用express.static并且已知可以在 Heroku 上工作 - 带有部署到 Heroku 按钮:
它比普通 Heroku 应用程序有更严格的要求。
查看该项目中的 package.json、server.js 和 app.json。
您应该能够拥有一个最小的 server.js 文件:
'use strict';
const path = require('path');
const express = require('express');
const http = require('http');
const app = express();
const server = http.Server(app);
const port = process.env.PORT || 8080;
app.use('/', express.static(path.join(__dirname, 'testheroku')));
server.listen(port, () => {
console.log(`Listening on http://localhost:${port}/`);
});
Run Code Online (Sandbox Code Playgroud)
或不直接http使用模块:
'use strict';
const path = require('path');
const app = require('express')();
const port = process.env.PORT || 8080;
app.use('/', express.static(path.join(__dirname, 'testheroku')));
app.listen(port, () => {
console.log(`Listening on http://localhost:${port}/`);
});
Run Code Online (Sandbox Code Playgroud)
但同样重要的是你的 package.json 包含一个启动脚本:
"scripts": {
"start": "node server.js"
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7803 次 |
| 最近记录: |