-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
31 lines (23 loc) · 945 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
// this db.js file is essentially responsible for establishinga connection between our node.js application and our mongodb database using the mongoose library
require('dotenv').config();
const mongoose = require('mongoose');
//define the mongodb connection url
const mongoURL = 'mongodb://127.0.0.1:27017/hotel'
//const mongoURL = process.env.MONGODB_URL
//set up mongo db connection
mongoose.connect(mongoURL) //establishes a mongodb connection to above url
//get the default conncetion
//mongoose maintains a default connection object representing the mongodb connection
const db = mongoose.connection;
//define event listners for db 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('disconnected from mongodb server')
})
//export the db connection
module.exports = db;