Skip to content

Commit

Permalink
Connect to database
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Traversy authored and Brad Traversy committed Jun 19, 2020
1 parent 996a7c5 commit 5731cd1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const express = require('express')
const dotenv = require('dotenv')
const connectDB = require('./config/db')

// Load config
dotenv.config({ path: './config/config.env' })

connectDB()

const app = express()

const PORT = process.env.PORT || 3000
Expand Down
18 changes: 18 additions & 0 deletions config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mongoose = require('mongoose')

const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})

console.log(`MongoDB Connected: ${conn.connection.host}`)
} catch (err) {
console.error(err)
process.exit(1)
}
}

module.exports = connectDB

0 comments on commit 5731cd1

Please sign in to comment.