1. Handle Ctrl-C
SIGINT is sent when the user presses Ctrl-C; handling it lets you clean up instead of dying abruptly.
process.on("SIGINT", shutdown) // Ctrl-CTyporithm · medium
Step 1 of 8 · intro card
CLI & Process
Listen for SIGINT (Ctrl-C) and SIGTERM (kill/orchestrators) to shut down gracefully — close servers and the database, then call process.exit(0) for success or a non-zero code for failure. The "exit" event is for final synchronous cleanup only; never start async work there.
process.on("SIGINT", ...) handles Ctrl-C and process.on("SIGTERM", ...) handles the signal orchestrators send; both give you a chance to shut down gracefully — close HTTP servers and database connections so in-flight requests complete instead of being severed.
process.exit(code) ends the process immediately with an exit code: 0 means success and any non-zero value means failure, which is how shells and CI pipelines decide whether your program succeeded.
process.on("exit", ...) fires as the process is already leaving, so only synchronous cleanup runs — any async work (awaits, timers, I/O) inside an exit handler is silently dropped, so do that draining in the signal handler instead.
SIGINT = Ctrl-C; SIGTERM = kill / orchestrators.
exit(0) = success; non-zero = failure.
The "exit" event runs only synchronous cleanup.
process.on("SIGINT", shutdown) // Ctrl-C
process.on("SIGTERM", shutdown) // kill / orchestrators
async function shutdown() {
await server.close()
await db.close()
process.exit(0) // 0 = success, non-zero = failure
}
process.on("exit", (code) => log(`exiting ${code}`))Connect each part of the code to the concept before you type.
SIGINT is sent when the user presses Ctrl-C; handling it lets you clean up instead of dying abruptly.
process.on("SIGINT", shutdown) // Ctrl-CSIGTERM is what kill and orchestrators like Kubernetes send to ask a process to stop gracefully.
process.on("SIGTERM", shutdown) // kill / orchestratorsGraceful shutdown means closing the server and database before exiting so in-flight work finishes.
await server.close()process.exit(0) signals success; any non-zero code signals failure to the calling shell.
process.exit(0) // 0 = success, non-zero = failureThe "exit" event fires as the process is leaving; only synchronous work runs here.
process.on("exit", (code) => log(`exiting ${code}`))Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.