Graceful shutdown lets your server close smoothly without dropping requests or losing data.
Graceful shutdown handling in Express
const express = require('express'); const app = express(); const port = 3000; const server = app.listen(port, () => { console.log(`Server running on port ${port}`); }); process.on('SIGINT', () => { console.log('Received SIGINT. Shutting down gracefully...'); server.close(() => { console.log('Closed out remaining connections.'); process.exit(0); }); // Optional: force exit after timeout setTimeout(() => { console.error('Could not close connections in time, forcefully shutting down'); process.exit(1); }, 10000); });
server.close() stops new connections but lets existing ones finish.
Listening to SIGINT lets you catch Ctrl+C to shut down nicely.
process.on('SIGTERM', () => { console.log('SIGTERM received, closing server'); server.close(() => { console.log('Server closed'); process.exit(0); }); });
server.close() to stop accepting new requests but finish current ones.server.close(() => {
console.log('Server stopped accepting new connections');
});This Express server listens on port 3000 and responds with 'Hello, world!'. When you press Ctrl+C, it catches the signal, stops accepting new requests, waits for current ones to finish, then exits cleanly.
const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello, world!'); }); const server = app.listen(port, () => { console.log(`Server running on port ${port}`); }); process.on('SIGINT', () => { console.log('Received SIGINT. Shutting down gracefully...'); server.close(() => { console.log('Closed out remaining connections.'); process.exit(0); }); setTimeout(() => { console.error('Could not close connections in time, forcefully shutting down'); process.exit(1); }, 10000); });
Always handle shutdown signals to avoid data loss or broken connections.
Use a timeout to force shutdown if connections hang too long.
Test shutdown by running the server and pressing Ctrl+C in the terminal.
Graceful shutdown helps your server stop safely without dropping requests.
Listen for signals like SIGINT or SIGTERM to trigger shutdown.
Use server.close() to finish current requests before exiting.
Practice
Solution
Step 1: Understand graceful shutdown concept
Graceful shutdown means the server stops accepting new requests but finishes processing ongoing ones.Step 2: Identify the purpose in Express context
In Express, this prevents dropping requests abruptly when stopping the server.Final Answer:
To allow the server to finish handling current requests before stopping -> Option AQuick Check:
Graceful shutdown = finish requests before stop [OK]
- Thinking it restarts the server automatically
- Confusing it with startup speed
- Believing it blocks all requests permanently
Solution
Step 1: Identify how Node.js listens for signals
Node.js uses process.on('signal', callback) to listen for OS signals like SIGINT.Step 2: Confirm Express does not have shutdown method
Express app or server does not have shutdown methods; signal handling is done on process.Final Answer:
process.on('SIGINT', () => { /* shutdown code */ }); -> Option DQuick Check:
Signal listening uses process.on [OK]
- Using server.listen for signals
- Calling shutdown on app or express object
- Confusing signal handling with server methods
const express = require('express');
const app = express();
const server = app.listen(3000);
process.on('SIGTERM', () => {
server.close(() => {
console.log('Server closed');
});
});
console.log('Server running');What will happen when the process receives a SIGTERM signal?
Solution
Step 1: Analyze signal handler code
On SIGTERM, server.close() is called with a callback that logs 'Server closed'.Step 2: Understand server.close behavior
server.close stops new connections but waits for ongoing requests to finish before calling the callback.Final Answer:
The server stops accepting new requests and logs 'Server closed' after finishing current requests -> Option AQuick Check:
SIGTERM triggers server.close with log after finish [OK]
- Assuming immediate crash on SIGTERM
- Thinking server restarts automatically
- Believing server ignores SIGTERM
const express = require('express');
const app = express();
const server = app.listen(3000);
process.on('SIGINT', () => {
server.close();
console.log('Server closed');
process.exit(0);
});What is the problem with this shutdown code?
Solution
Step 1: Understand server.close asynchronous behavior
server.close is asynchronous and takes a callback to run after closing.Step 2: Identify premature exit call
process.exit(0) is called immediately after server.close without waiting, so server may not close properly.Final Answer:
It calls process.exit(0) immediately, not waiting for server.close to finish -> Option BQuick Check:
Must wait for server.close callback before exit [OK]
- Thinking SIGINT is invalid signal
- Confusing app.close with server.close
- Ignoring asynchronous nature of server.close
Solution
Step 1: Handle SIGINT signal properly
Listen for SIGINT to start graceful shutdown process.Step 2: Close server and then database sequentially
Call server.close() to finish requests, then in its callback close the database connection.Step 3: Exit process after all resources are closed
Call process.exit() only after both server and database are closed to avoid abrupt termination.Final Answer:
Listen for SIGINT, call server.close(), then close database in server.close callback, finally call process.exit() -> Option CQuick Check:
Shutdown sequence: server.close -> db.close -> exit [OK]
- Exiting process before closing resources
- Ignoring database closure
- Restarting server instead of shutting down
Ready to prove it?
Pass the test and this pattern moves unknown
