🚀
Auth, Testing & Deployment

Production Deployment

PM2, Cluster aur Reverse Proxy
💡 Production deployment EK DUKAAN ko PROPER SHOWROOM banane jaisa hai — ek hi salesman (single process) ke bajaye poori team (cluster), guard aur reception (reverse proxy), aur koi gir jaaye to turant replacement (PM2 auto-restart).

Node EK CPU CORE use karta hai — cluster module ya PM2 ka cluster mode multiple worker processes chalakar saare cores use karta hai aur throughput badhata hai. PM2 crash par auto-restart, zero-downtime reload aur log management bhi deta hai.

Express ko DIRECTLY internet par expose karne ke bajaye Nginx jaise reverse proxy ke peeche rakho — wo TLS termination, static files, aur load balancing behtar karta hai. Peeche rakhne par app.set("trust proxy", 1) zaroori hai taaki req.ip aur secure cookies sahi kaam karein.

pm2 start server.js -i max --name api    # cluster mode, saare cores
pm2 reload api                            # zero-downtime restart
pm2 logs api

app.set("trust proxy", 1);                // Nginx ke peeche
app.get("/health", (req, res) => res.json({ status: "ok" }));
🚀
Production deployment EK DUKAAN ko PROPER SHOWROOM banane jaisa hai — ek hi salesman (single process) ke bajaye poori team (cluster), guard aur reception (reverse proxy), aur koi gir jaaye to turant replacement (PM2 auto-restart).
1 / 2
⚡ Quick Recap
  • PM2/cluster se multiple cores use hote hain aur crash par auto-restart milta hai
  • Nginx jaisa reverse proxy TLS, static files aur load balancing sambhaale
  • trust proxy set karo aur health endpoint expose karo
On this page (2 subtopics)

Node ek core use karta hai, isliye 4-core machine par ek process 75% capacity waste karta hai. Cluster mode kai worker processes chalata hai jo ek hi port share karte hain — OS load balance karta hai. PM2 ke -i max se ye ek command mein ho jaata hai.

pm2 start server.js -i max --name api
pm2 status
pm2 reload api            # zero-downtime — ek-ek worker restart
pm2 startup && pm2 save   # machine reboot par auto-start

Deploy ya scale-down ke waqt SIGTERM aata hai. Turant exit karne se chal rahi requests toot jaati hain — sahi tareeka hai naye connections lena band karo, chal rahi requests poori karo, DB connections band karo, phir exit. Health endpoint orchestrator ko batata hai ki instance traffic ke liye ready hai.

const server = app.listen(config.port);
process.on("SIGTERM", () => {
  server.close(async () => {
    await db.close();
    process.exit(0);
  });
  setTimeout(() => process.exit(1), 10000).unref();   // force after 10s
});