Skip to content

Instantly share code, notes, and snippets.

@yvesbou
Created February 18, 2022 10:25
Show Gist options
  • Save yvesbou/60f3a4bbee7e5577153ae1b21b7e4b16 to your computer and use it in GitHub Desktop.
Save yvesbou/60f3a4bbee7e5577153ae1b21b7e4b16 to your computer and use it in GitHub Desktop.
require('dotenv').config();
const mongoose = require('mongoose');
const DATABASE_URL = process.env.DATABASE_URL;
const connectDB = () => {
return mongoose.connect(DATABASE_URL, {useUnifiedTopology: true, useNewUrlParser: true}, err => {
if (err){
console.error('Connection to DB failed');
} else{
console.log('Connection to DB was successful');
}
})
}
const db = mongoose.connection;
db.on('error', console.error.bind(console, "MongoDB connection failed"));
module.exports = connectDB;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment