-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect.js
More file actions
28 lines (22 loc) · 797 Bytes
/
connect.js
File metadata and controls
28 lines (22 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const mongoose = require("mongoose");
const redis = require("redis");
// Use local MongoDB if you're not using Atlas
const mongoURI = "mongodb://127.0.0.1:27017/short-url";
// Correct Redis connection
const redisClient = redis.createClient({ url: "redis://127.0.0.1:6380" });
redisClient.on("error", (err) => console.error("Redis Error:", err));
async function connectToMongoDB() {
try {
await mongoose.connect(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("✅ MongoDB Connected");
await redisClient.connect();
console.log("✅ Redis Connected");
} catch (error) {
console.error("❌ Connection Error:", error);
process.exit(1); // Stop the app if DB fails
}
}
module.exports = { connectToMongoDB, redisClient };