【问题标题】:Heroku error node, mongo, react (Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch)Heroku 错误节点,mongo,react(错误 R10(启动超时)-> Web 进程未能在启动后 60 秒内绑定到 $PORT)
【发布时间】:2021-04-11 20:19:33
【问题描述】:

这是我第一次在 Heroku 上部署应用程序。它在本地运行良好,但是一旦我尝试在 Heroku 上打开应用程序,我就会收到“应用程序错误”。我将在下面添加代码文件。

过程文件

web: npm run start

index.js

import express from "express";
import bodyParser from "body-parser";
import mongoose from "mongoose";
import cors from "cors";
import dotenv from "dotenv";
import postRoutes from "./routes/posts.js";

const app = express();
dotenv.config();

app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
app.use(cors());

app.use("/posts", postRoutes);

app.get("/", (req, res) => {
  res.send('Hello to Memories API');
});

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

mongoose
  .connect(process.env.CONNECTION_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() =>
    app.listen(PORT, () => console.log(`Server running on port: ${PORT}`))
  )
  .catch((error) => console.log(error.message));

mongoose.set("useFindAndModify", false);

.env

CONNECTION_URL = mongodb+srv://<username>:<password>@cluster0.a0uak.mongodb.net/<dbname>?retryWrites=true&w=majority

package.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "heroku": "^7.47.7",
    "mongoose": "^5.11.8",
    "n": "^7.0.0",
    "nodemon": "^2.0.6"
  }
}

我还确认我的 IP 地址已在 MongoDB 站点上列入白名单。

希望有人能帮我解决这个问题。

【问题讨论】:

    标签: node.js mongodb express heroku mongoose


    【解决方案1】:

    上面error明确提到web process绑定分配的$PORT花费的时间比60 second要长。这个error一般发生在进程无法访问DB等外部资源的时候。但是,默认的boot 时间是60 second。但是有些应用程序需要更多时间来boot。您可以尝试增加应用程序的启动超时。 Go to this.

    【讨论】:

    • 感谢您抽出宝贵时间帮助我解决此问题,并对延迟回复表示歉意。我已将启动时间提高到 150 秒,这是 Heroku 的最大时间限制,同样的问题仍在继续。
    • 将此command 输入您的procfile 一次bundle exec thin start -p $PORT
    • 你真是个天才!!这解决了我的问题!如果您能解释一下,我想进一步了解它的作用。
    猜你喜欢
    • 2021-04-05
    • 2021-08-19
    • 2020-12-09
    • 2019-07-04
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 2019-04-20
    • 2015-09-14
    相关资源
    最近更新 更多