-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
37 lines (26 loc) · 890 Bytes
/
db.js
File metadata and controls
37 lines (26 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const mongoose = require('mongoose');
require('dotenv').config();
// Define the MongoDB connection URL
// const mongoURL = process.env.LOCAL_HOST //
// Hosting server on MongoDB atlas
const mongoURL = process.env.DB_HOST_URL;
// Set up MongoDB connection (not needed any longer for the lastest versions)
mongoose.connect(mongoURL,{
useNewUrlParser: true,
useUnifiedTopology: true
})
// Get the default connection
// Mongoose maintains a default connection object representing the MongoDB connection.
const db = mongoose.connection;
// Define event listeners for database connection
db.on('connected', () => {
console.log('Connected to MongoDB server !');
})
db.on('error', (err) => {
console.log('MongoDB connection error', err);
})
db.on('disconnected', () => {
console.log('MongoDB disconnected');
})
// Export the database connection
module.exports = db;