【发布时间】: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,它也在根文件夹中