backend-mongodb-setup
v1.0.2
Published
Sets up MongoDB connection and updates app.js
Downloads
9
Readme
📜 README for backend-mongodb-setup
📌 About
backend-mongodb-setup is an NPM package that helps set up MongoDB connection in a Node.js backend.
🚀 Installation
Run the following command inside your backend project:
npx backend-mongodb-setup📌 What This Package Does
1️⃣ Creates a src/config/database.js file.
2️⃣ Ensures .env contains MongoDB connection string.
3️⃣ Connects MongoDB using Mongoose.
📝 database.js
const mongoose = require('mongoose');
require('dotenv').config();
const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('✅ MongoDB Connected');
} catch (error) {
console.error('❌ MongoDB Connection Error:', error);
process.exit(1);
}
};
module.exports = connectDB;🔧 Updates .env File
If .env already exists, it updates it without removing existing data.
MONGO_URI=mongodb://localhost:27017/mydatabase✅ Features
✔ Handles MongoDB connection setup
✔ Automatically updates .env file
✔ Uses Mongoose for easy database interactions
