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