【发布时间】: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