From 8d6a3d4d9b438d02a1ca5e8d9f851935c228b9f3 Mon Sep 17 00:00:00 2001 From: rohit <153482419+mr-saadhak@users.noreply.github.com> Date: Fri, 27 Feb 2026 18:03:11 +0530 Subject: [PATCH] feat: production server error handling --- server.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 7 deletions(-) diff --git a/server.js b/server.js index 5d1741d..3f5a2ae 100644 --- a/server.js +++ b/server.js @@ -1,14 +1,69 @@ import 'dotenv/config'; +import http from 'http'; import app from './src/app.js'; import connectDB from './src/config/db.js'; +import { start } from 'repl'; const PORT = process.env.PORT || 5000; +const NODE_ENV = process.env.NODE_ENV || 'development'; -connectDB().then(() => { - app.listen(PORT, () => { - console.log(`Server running on port ${PORT}`); - console.log("Deployment"); - }); -}).catch((error) => { - console.log(`Error: ${error}`); +let server; + +const startServer = async() => { + try { + if (!process.env.MONGO_URI) { + throw new Error("MONGO_URI is missing in environment variables"); + } + + if (!process.env.JWT_SECRET) { + throw new Error("JWT_SECRET is missing in environment variables"); + } + + await connectDB(); + console.log("DB connected successfully"); + + server = http.createServer(app); + + server.listen(PORT, () => { + console.log(`Server running in ${NODE_ENV} mode on port ${PORT}`); + }); + } catch(error) { + console.error("Startup error: ", error.message); + process.exit(1); + } +}; + +startServer(); + +process.on("unhandledRejection", (err) => { + console.error("Unhandled Rejection: ", err); + shutdownGracefully(); }); + +process.on("uncaughtException", (err) => { + console.error("Uncaught Exception: ", err); + shutdownGracefully(); +}); + +const shutdownGracefully = () => { + if (server) { + server.close(() => { + console.log("Server closed grecefully"); + process.exit(1); + }); + } + else { + process.exit(1); + } +}; + +process.on("SIGTERM", () => { + console.log("SIGTERM received. Shutting down..."); + shutdownGracefully(); +}); + +process.on("SIGINT", () => { + console.log("SIGINT reveiced. Shutting down..."); + shutdownGracefully(); +}); +