admin管理员组文章数量:1025457
I setup a MongoDB cluster using MongoDB atlas. After this I created an express app by running the npm init -y
.
I then installed some packages and set the scripts. Here is my package.json
:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/index.js",
"start": "node src/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.1",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.21.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.8.1",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.1.7"
}
}
After setting this up I managed to connect to MongoDB using mongoose.
Then I ran npm i axios
and I got the following error when I ran npm run dev
:
Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: /
At this moment I have enabled allow access from anywhere
on my MongoDB cluster. So I do not know what is happening.
Here is my index.js:
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(express.json())
app.use(cors({
origin: '*',
credentials: true
}));
app.use(bodyParser.json());
mongoose.connect(process.env.MONGODB_URI);
mongoose.connection.on('connected', () => {
console.log('Connected to MongoDB');
})
mongoose.connection.on('error', (err) => {
console.log('Error connecting to MongoDB', err);
});
app.get('/', (req, res) => {
res.send('API');
});
app.listen(process.env.PORT, () => {
console.log(`API listening at http://localhost:${process.env.PORT}`);
});
module.exports = app;
I tried testing this with other packages too, but it happens everytime I install ANY package.
Any help would be appreciated!
I setup a MongoDB cluster using MongoDB atlas. After this I created an express app by running the npm init -y
.
I then installed some packages and set the scripts. Here is my package.json
:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/index.js",
"start": "node src/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.1",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.21.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.8.1",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.1.7"
}
}
After setting this up I managed to connect to MongoDB using mongoose.
Then I ran npm i axios
and I got the following error when I ran npm run dev
:
Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: /
At this moment I have enabled allow access from anywhere
on my MongoDB cluster. So I do not know what is happening.
Here is my index.js:
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(express.json())
app.use(cors({
origin: '*',
credentials: true
}));
app.use(bodyParser.json());
mongoose.connect(process.env.MONGODB_URI);
mongoose.connection.on('connected', () => {
console.log('Connected to MongoDB');
})
mongoose.connection.on('error', (err) => {
console.log('Error connecting to MongoDB', err);
});
app.get('/', (req, res) => {
res.send('API');
});
app.listen(process.env.PORT, () => {
console.log(`API listening at http://localhost:${process.env.PORT}`);
});
module.exports = app;
I tried testing this with other packages too, but it happens everytime I install ANY package.
Any help would be appreciated!
本文标签: expressCan39t connect to MongoDB with mongoose after installing packageStack Overflow
版权声明:本文标题:express - Can't connect to MongoDB with mongoose after installing package - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745622554a2159664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论