我回到了一段时间没有碰到的项目,却遇到了这个错误。
MongoNetworkError:首次连接[MongoNetworkError:到cluster0-shard-00-01-ntrwp.mongodb.net的连接5时,无法连接到服务器[cluster0-shard-00-01-ntrwp.mongodb.net:27017]: 27017已关闭
我检查了连接网络的访问权限,并让每个人都对mongoose.connect CREDENTIAL进行了两次检查,但我不明白为什么它不断向我发送此错误。我还重新安装了npm猫鼬。
const fs = require("fs");
const path = require("path");
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const placesRoutes = require("./routes/places-routes");
const usersRoutes = require("./routes/users-routes");
const HttpError = require("./models/http-error");
const app = express();
app.use(bodyParser.json());
app.use("/uploads/images", express.static(path.join("uploads", "images")));
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE");
next();
});
app.use("/api/places", placesRoutes);
app.use("/api/users", usersRoutes);
app.use((req, res, next) => {
const error = new HttpError("Could not find this route.", 404);
throw error;
});
app.use((error, req, res, next) => {
if (req.file) {
fs.unlink(req.file.path, (err) => {
console.log(err);
});
}
if (res.headerSent) {
return next(error);
}
res.status(error.code || 500);
res.json({ message: error.message || "An unknown error occurred!" });
});
mongoose
.connect(
`mongodb+srv://XXXX:XXXX@cluster0-ntrwp.mongodb.net/XXXX?retryWrites=true&w=majority`
)
.then(() => {
app.listen(5000);
})
.catch((err) => {
console.log(err);
});