上传到 Heroku 时出现应用程序错误

Application Error while uploading to Heroku

对于我的应用程序后端,我尝试在 heroku 上托管。在 heroku 上创建新应用程序后,我按照它向我展示的所有步骤进行操作并成功部署。但是当我转到 link 时,它给我应用程序错误。我试图通过 heroku logs --tail 获取日志,它给了我这个

2021-12-29T07:57:12.741895+00:00 app[web.1]: Error:  The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
2021-12-29T07:57:12.751654+00:00 app[web.1]: [nodemon] clean exit - waiting for changes before restart
2021-12-29T07:58:11.201323+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2021-12-29T07:58:11.236815+00:00 heroku[web.1]: Stopping process with SIGKILL
2021-12-29T07:58:11.259774+00:00 app[web.1]: Error waiting for process to terminate: No child processes
2021-12-29T07:58:11.400798+00:00 heroku[web.1]: Process exited with status 22
2021-12-29T07:58:11.473743+00:00 heroku[web.1]: State changed from starting to crashed
2021-12-29T08:56:18.088383+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=pin-map-app.herokuapp.com request_id=30848095-cca3-4afe-b1ac-705529a0b023 fwd="103.166.88.18" dyno= connect= service= status=503 bytes= protocol=https
2021-12-29T08:56:19.043794+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=pin-map-app.herokuapp.com request_id=dedb7d05-a475-4e11-972d-e484b397c88e fwd="103.166.88.18" dyno= connect= service= status=503 bytes= protocol=https

如您所见,openUri() 存在一些问题,为此我已经设置了 dotenv 并在我的 index.js 中添加了 dotenv.config()。我尝试 heroku restart 并再次推送所有代码,但问题仍然存在。 index.js

import express from "express";
import mongoose from "mongoose";
import pinRoutes from "./routes/pins.js";
import userRoutes from "./routes/users.js";
import cors from "cors";
import dotenv from "dotenv";

dotenv.config();
const app = express();
app.use(express.json({ limit: "30mb", extended: true }));
app.use(cors());
app.use("/pins", pinRoutes);
app.use("/user", userRoutes);

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

app.get("/", (req, res) => {
  res.send("welcome to pin it");
});

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

在我的 .env 文件中,我只有 CONNECTION_URL,没有别的。我以前部署过一些其他应用程序,但这是我第一次遇到此类错误。

首先:确保所有的独立都安装在package.json

其次:

尽管您设置了正确的环境,但它向您显示 mongoose 错误表明您的第一个 错误参数,尝试做直接链接,without .env

只需确保其中连接的端口是:

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

this just makes the port that connected dynamic
If port 3000 does not work will switch to another one.  

听如下:

app.listen(port)