This repository was archived by the owner on Jun 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (60 loc) · 2.33 KB
/
Copy pathserver.js
File metadata and controls
71 lines (60 loc) · 2.33 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const express = require("express");
const app = express();
const MongoClient = require("mongodb").MongoClient;
const PORT = process.env.PORT || 5050;
const MONGO_URL = process.env.MONGO_URL || "mongodb://mongo-service:27017";
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));
const client = new MongoClient(MONGO_URL);
let db;
// Health check
app.get("/health", (req, res) => {
res.json({ status: "ok", db: db ? "connected" : "disconnected" });
});
// GET all users
app.get("/getUsers", async (req, res) => {
if (!db) return res.status(503).json({ error: "Database not ready yet, try again in a moment." });
const data = await db.collection("users").find({}).toArray();
res.json(data);
});
// POST new user
app.post("/addUser", async (req, res) => {
if (!db) return res.status(503).json({ error: "Database not ready yet, try again in a moment." });
const userObj = req.body;
if (!userObj || Object.keys(userObj).length === 0) {
return res.status(400).json({ error: "Request body is empty." });
}
console.log(userObj);
await db.collection("users").insertOne(userObj);
console.log("Data inserted in DB");
res.json({ message: "User Added Successfully" });
});
// Only start listening when run directly (not when imported by tests)
if (require.main === module) {
app.listen(PORT, () => console.log(`server running on port ${PORT}`));
connectDB();
}
module.exports = { app, connectDB, setDb: (d) => { db = d; } };
// Connect to MongoDB, then start server
// Retries every 3 seconds so it survives the mongo pod being slow to start
async function connectDB() {
for (let attempt = 1; attempt <= 10; attempt++) {
try {
await client.connect();
console.log("Connected successfully to MongoDB");
db = client.db("pallavi-db");
return; // success
} catch (err) {
console.error(`MongoDB connection attempt ${attempt} failed: ${err.message}`);
if (attempt < 10) {
console.log("Retrying in 3 seconds...");
await new Promise(res => setTimeout(res, 3000));
} else {
console.error("Could not connect to MongoDB after 10 attempts. Exiting.");
process.exit(1);
}
}
}
}
connectDB();