Production Node.js · 8 min read · Intermediate
Structured Logging & Graceful Shutdown
Emit machine-parseable JSON logs and handle SIGTERM/SIGINT so deployments finish active requests cleanly.
What you'll learn
- Emit structured JSON logs with timestamp, level, and request context
- Listen for SIGTERM and SIGINT to close HTTP servers and DB pools gracefully
- Understand why zero-downtime deploys depend on graceful shutdown
When Docker, Kubernetes, or systemd restarts your Node.js server during a deploy, it sends SIGTERM. Listening for process.on("SIGTERM", ...) and calling server.close() lets in-flight HTTP requests finish cleanly instead of dropping connections mid-response.
Fill-in-the-Blank Exercise
Handle SIGTERM Gracefully
Register a SIGTERM listener on process to close the HTTP server.
.on("", () => {
server.close(() => process.exit(0));
});Live Code Lab (Node.js 20)
Node.jsstructured_log.js
Expected OutputClick “Run Live” to compile with Node.js 20
{"level":"info","event":"server_started","port":8787}
{"level":"info","event":"http_request","method":"POST","path":"/api/run","status":200,"ms":48}Check Your Understanding
Score: 0 / 1
1. Why should production Node.js servers listen for the SIGTERM signal?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.