【发布时间】:2022-01-22 20:08:32
【问题描述】:
我正在尝试 npm 启动我的后端,但连接到 mongodb atlas 时出现错误。 这是我的 index.js 代码:
import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import cors from 'cors';
const app = express();
app.use(bodyParser.json({limit: "30mb", extended: true}));
app.use(bodyParser.urlencoded({limit: "30mb", extended: true}));
app.use(cors());
const CONNECTION_URL = "mongodb+srv://Danishbukhari:(mypassword)@cluster0.dxzhf.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
const PORT = process.env.PORT || 5000;
mongoose.connect(CONNECTION_URL, { useNewUrlParser: true})
.then(() => app.listen(PORT, () => console.log(`Server running on port: ${PORT}`)))
.catch((error) => console.log(error.message) )
我的 package.json 就像:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "nodemon index.js"
},
"author": "Danish",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.1",
"cors": "^2.8.5",
"express": "^4.17.2",
"mongodb": "^4.2.2",
"mongoose": "^6.1.2",
"nodemon": "^2.0.15"
}
}
我也将我的 IP 地址列入了白名单 请告诉我如何解决这个问题:(
【问题讨论】:
标签: node.js mongodb mongoose mongodb-atlas