Skip to content

Instantly share code, notes, and snippets.

@NGR-NP
Last active January 27, 2023 07:57
Show Gist options
  • Save NGR-NP/076f8463be75a0c776e9f21647f512e8 to your computer and use it in GitHub Desktop.
Save NGR-NP/076f8463be75a0c776e9f21647f512e8 to your computer and use it in GitHub Desktop.
connect to mongodb with mongoose
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