1 // Create .env file
2 inside .env
3 MONGO_URI=mongo+src://username:password@cluster0......majority
4 //
5 // Inside app.js or server.js
6 const dotenv = require('dotenv')
7 dotenv.config()
8
9 const mongoose = require('mongoose')
10
11 mongoose.connect(process.env.MONGO_URI, {
12 useNewUrlParser: true,
13 useUnifiedTopology: true
14 });
15 const db = mongoose.connection
16 db.on('error', error => console.error(error))
17 db.once('open', MONGO_URI => console.log('Connected to Database'))
18
1mongoose.connect(process.env.MONGO_URI).then(() => {
2 console.log("DB Connected");
3});
4
5mongoose.connection.on("error", err => {
6 console.log(`DB Connection Error: ${err.message}`);
7});
8