I am having an error in my node server. I am using mongoose as an ORM with node.js and I am trying to connect to MongoDB Atlas and at first I am able to connect to my cluster but after some seconds I am getting this error in my console as,
Shutting down... Error: Error connecting to db: connect ECONNREFUSED ::1:27017
I am also able to connect to my cluster with Compass. I am really getting confused why I am getting this error as this has nothing to do with my localhost MongoDB sever but still I have enabled my server at localhost too just to check whether it can solve anything or not but really it didn’t
For more specifications:
Node js version: v18.12.1
MongoDB version: MongoDB shell version v4.4.18
Mongoose version: ^5.13.14,
OS: Pop_os(22.04)
here is my code of my server.js where I am trying to connect to my MongoDB cluster.
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const server = require('./app');
process.on('uncaughtException', (err) => {
console.log('UNCAUGHT EXCEPTION! Shutting down...', err);
process.exit(1);
});
process.on('unhandledRejection', (err) => {
console.log('UNHANDLED REJECTION! Shutting down...', err);
process.exit(1);
});
dotenv.config({path: './config.env'});
const PORT = process.env.PORT || 5000;
const DB_PASS = process.env.DB_PASSWORD;
const DB_URL = process.env.DB_URL.replace('<password>', DB_PASS);
mongoose
.connect(DB_URL, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
})
.then(() => console.log('Database sucessfully connected'));
exports.sessionUrl = DB_URL;
server.listen(PORT, () => {
console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`);
});