-
Notifications
You must be signed in to change notification settings - Fork 21
/
db.js
34 lines (25 loc) · 860 Bytes
/
db.js
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
const mongoose = require('mongoose');
require('dotenv').config();
// Define the MongoDB connection URL
const mongoURL = process.env.MONGODB_URL_LOCAL // Replace 'mydatabase' with your database name
// const mongoURL = process.env.MONGODB_URL;
// Set up MongoDB connection
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.error('MongoDB connection error:', err);
});
db.on('disconnected', () => {
console.log('MongoDB disconnected');
});
// Export the database connection
module.exports = db;