Last active
January 27, 2023 07:57
-
-
Save NGR-NP/076f8463be75a0c776e9f21647f512e8 to your computer and use it in GitHub Desktop.
connect to mongodb with mongoose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require("mongoose"); | |
const MONGO_URI = mongodb+srv://<username>:<password>@projectname.dmcuzcx.mongodb.net/collectionname | |
const connectMongoDB = () => { | |
try { | |
mongoose.connect(MONGO_URI); | |
console.log("\n Waiting β° MongoDB Connections π΄ π΄"); | |
} catch (error) { | |
throw error; | |
} | |
}; | |
mongoose.connection.on("disconnected", () => { | |
console.log("\nββββββββββββββ"); | |
console.log(" π MongoDB Disconnected β"); | |
console.log("ββββββββββββββ"); | |
}); | |
mongoose.connection.on("connected", () => { | |
console.log(); | |
console.log("π π π MongoDB connected successfullyπ₯³ π π π"); | |
}); | |
module.exports = connectMongoDB; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment