【问题标题】:Why does my heroku app keep crashing even if it works locally?为什么我的 Heroku 应用程序即使在本地运行也会不断崩溃?
【发布时间】:2020-08-04 15:29:47
【问题描述】:

我一直在网上到处寻找,但似乎找不到答案。我不断收到状态为 503 的错误代码 H10,这意味着它由于请求超时而崩溃。但是,我无法确定问题出在哪里。

这是我的 server.js 文件:

const mongoose = require("mongoose");
const logger = require("morgan");

const PORT = process.env.PORT || 3000;

const app = express();

app.use(logger("dev"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));

require("./routes/apiRoutes.js")(app);
require("./routes/htmlRoutes.js")(app);

mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/workout", { useNewUrlParser: true });

app.listen(3000, () => {
    console.log(`App running on port ${PORT}!`);
});

这是最近的错误日志:

2020-04-21T18:54:38.462341+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=protected-depths-80302.herokuapp.com request_id=74846a7d-d9b0-49b7-a8ee-979d3bb75d6a fwd="73.248.13.42" dyno= connect= service= status=503 bytes= protocol=https
2020-04-21T18:54:38.678122+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=protected-depths-80302.herokuapp.com request_id=5e304f76-b764-4984-baa9-bffea70d9bcf fwd="73.248.13.42" dyno= connect= service= status=503 bytes= protocol=https

我的 server.js 文件中是否缺少某些内容?

【问题讨论】:

    标签: node.js heroku mongoose


    【解决方案1】:

    使用通过 process.env.PORT 变量设置的端口变量(或 3000 未定义,即在本地工作)

    app.listen(PORT, () => {
    console.log(`App running on port ${PORT}!`);
    });
    

    【讨论】:

      猜你喜欢
      • 2020-12-22
      • 2020-08-30
      • 2012-01-28
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 1970-01-01
      相关资源
      最近更新 更多