Description
The server continues to start even when MongoDB connection fails, leading to degraded functionality without proper error handling.
Location
File: backend/server.js:78-88
connectDB()
.then((success) => {
if (success) {
console.log("MongoDB connected successfully");
} else {
console.warn("⚠️ Failed to connect to MongoDB - server will run without database connection");
}
})
.catch((err) => {
console.error("Database connection error:", err.message);
});
// Server starts here even if DB failed...
const server = app.listen(PORT, "0.0.0.0", () => {
// Server runs without database
});
Root Cause
- Server starts regardless of MongoDB connection status
- No health check for database availability
- API endpoints return confusing errors when DB is down
Impact
- Users get cryptic errors instead of clear service unavailable message
- Background jobs may fail silently
- Data inconsistency if writes are attempted
- Difficult debugging in production
Recommended Fix
// Block server startup until DB is ready (or timeout)
async function startServer() {
try {
await connectDB();
console.log("MongoDB connected successfully");
} catch (err) {
console.error("FATAL: Cannot connect to MongoDB:", err.message);
console.error("Exiting - database is required for this service");
process.exit(1);
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on port ${PORT}`);
});
}
startServer();
Severity
HIGH - Operational reliability issue
Description
The server continues to start even when MongoDB connection fails, leading to degraded functionality without proper error handling.
Location
File:
backend/server.js:78-88Root Cause
Impact
Recommended Fix
Severity
HIGH - Operational reliability issue