【问题标题】:Application Error while uploading to Heroku上传到 Heroku 时出现应用程序错误
【发布时间】:2022-04-22 07:40:05
【问题描述】:

对于我的应用程序后端,我正在尝试在 heroku 上进行托管。在 heroku 上创建一个新应用程序后,我已经按照它向我展示的所有步骤进行了操作,并且部署成功。但是当我转到链接时,它给了我应用程序错误。我试图通过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,没有别的。我之前部署了一些其他应用程序,但这是我第一次遇到此类错误。

【问题讨论】:

  • 您的 .env 文件是否在您的根文件夹中?你检查过你的 procfile 了吗?
  • @Pandamora 是的,env 文件在根文件夹中,在 Procfile 中我添加了web: npm run start,它也在根文件夹中

标签: node.js heroku


【解决方案1】:

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

其次:

尽管您设置了正确的环境,显示mongoose 错误告诉您的第一个参数有错误,尝试进行直接链接,没有 .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)

【讨论】:

  • 删除 env 文件并直接放置连接链接为我完成了工作。但我不明白为什么会这样。
  • 有一个原因,URL 没有链接。这意味着您的 .env 文件不起作用
猜你喜欢
  • 2013-02-15
  • 2019-03-31
  • 2021-03-25
  • 2014-07-23
  • 2018-05-30
  • 2014-09-08
  • 1970-01-01
  • 2022-09-25
  • 2022-01-05
相关资源
最近更新 更多