diff --git a/mongodb.js b/mongodb.js index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..783d98c786b2251bf5b77f379d3971ae7ec07e83 100644 --- a/mongodb.js +++ b/mongodb.js @@ -0,0 +1,15 @@ +const mongoose = require('mongoose'); + +// Function to connect to MongoDB using Mongoose and create collections +async function connectDB() { + try { + // Connect using the MongoDB URI from environment variables + const conn = await mongoose.connect(process.env.MONGO_URI || "mongodb://127.0.0.1:27017/todo_app"); + console.log(`MongoDB connected: ${conn.connection.host}`); + } catch (err) { + console.error(`Error: ${err.message}`); + process.exit(1); // Exit the process with failure if the connection fails + } +} + +module.exports = connectDB; \ No newline at end of file