📖 Scenario: You are building a simple Express server that needs to stop cleanly when the app is closed or restarted. This means it should finish any ongoing requests before shutting down.
🎯 Goal: Create an Express server that listens on port 3000 and implements graceful shutdown by closing the server and exiting the process when it receives a termination signal.
📋 What You'll Learn
Create an Express app with a basic route
Start the server listening on port 3000
Add a variable to hold the server instance
Implement a function to handle graceful shutdown
Listen for termination signals and call the shutdown function
💡 Why This Matters
🌍 Real World
Servers often need to stop without dropping active users or leaving resources open. Graceful shutdown helps keep apps reliable and user-friendly.
💼 Career
Understanding graceful shutdown is important for backend developers and DevOps engineers to maintain stable and maintainable server applications.
Progress0 / 4 steps
1
Create the Express app and basic route
Create an Express app by requiring express and calling it to create app. Then add a GET route on '/' that sends the text 'Server is running'.
Express
Hint
Use require('express') to import Express and app.get to create a route.
2
Start the server and save the server instance
Start the server listening on port 3000 by calling app.listen(3000). Save the returned server instance in a variable called server.
Express
Hint
Save the result of app.listen(3000) to server.
3
Create a graceful shutdown function
Create a function called gracefulShutdown that calls server.close() with a callback. Inside the callback, call process.exit(0) to exit the process cleanly.
Express
Hint
Use server.close() with a callback that calls process.exit(0).
4
Listen for termination signals and call graceful shutdown
Use process.on to listen for the 'SIGINT' and 'SIGTERM' signals. For each, call the gracefulShutdown function.
Express
Hint
Use process.on to listen for 'SIGINT' and 'SIGTERM' and call gracefulShutdown.
Practice
(1/5)
1. What is the main purpose of implementing graceful shutdown in an Express server?
easy
A. To allow the server to finish handling current requests before stopping
B. To restart the server automatically after a crash
C. To speed up server startup time
D. To block all incoming requests permanently
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 A
Quick Check:
Graceful shutdown = finish requests before stop [OK]
Hint: Graceful shutdown means finishing requests before exit [OK]
Common Mistakes:
Thinking it restarts the server automatically
Confusing it with startup speed
Believing it blocks all requests permanently
2. Which of the following is the correct way to start listening for a shutdown signal in an Express app?
easy
A. express.shutdown('SIGINT', () => { /* shutdown code */ });
B. server.listen('SIGINT', () => { /* shutdown code */ });
C. app.shutdown('SIGINT', () => { /* shutdown code */ });
D. process.on('SIGINT', () => { /* shutdown code */ });
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.
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 B
Quick Check:
Must wait for server.close callback before exit [OK]
Hint: Wait for server.close callback before exiting process [OK]
Common Mistakes:
Thinking SIGINT is invalid signal
Confusing app.close with server.close
Ignoring asynchronous nature of server.close
5. You want to implement graceful shutdown in an Express app that also uses a database connection. Which approach correctly ensures both server and database close properly before exiting?
hard
A. Call process.exit() immediately on SIGINT, then close server and database asynchronously
B. Close database connection first, then call server.close(), ignoring process signals
C. Listen for SIGINT, call server.close(), then close database in server.close callback, finally call process.exit()
D. Restart the server automatically on SIGINT without closing connections
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 C