0
0
Expressframework~20 mins

Graceful shutdown handling in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Graceful Shutdown Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the server receives SIGINT in this Express app?
Consider this Express server code that listens for SIGINT to shut down gracefully. What will be the console output if you press Ctrl+C to stop the server?
Express
const express = require('express');
const app = express();
const server = app.listen(3000, () => console.log('Server started'));

process.on('SIGINT', () => {
  console.log('SIGINT received');
  server.close(() => {
    console.log('Server closed');
    process.exit(0);
  });
});
ASIGINT received\nServer started\nServer closed
BServer started\nServer closed\nSIGINT received
CServer started\nSIGINT received
DServer started\nSIGINT received\nServer closed
Attempts:
2 left
💡 Hint
Think about the order of events when SIGINT is caught and the server closes.
📝 Syntax
intermediate
2:00remaining
Which option correctly attaches a listener for graceful shutdown in Express?
You want to listen for the SIGTERM signal to gracefully close your Express server. Which code snippet is syntactically correct and will work as intended?
Aprocess.on('SIGTERM', () => server.close(() => process.exit(0)));
Bprocess.addListener('SIGTERM', server.close(() => process.exit(0)));
Cprocess.on('SIGTERM', server.close(() => process.exit(0)));
Dprocess.on('SIGTERM', () => { server.close; process.exit(0); });
Attempts:
2 left
💡 Hint
Remember that the listener callback should be a function, not the result of calling a function.
🔧 Debug
advanced
2:00remaining
Why does this Express server not exit on SIGINT?
This Express server code listens for SIGINT but never exits. What is the cause?
Express
const express = require('express');
const app = express();
const server = app.listen(3000);

process.on('SIGINT', () => {
  console.log('SIGINT caught');
  server.close();
  console.log('Server closed');
});
Aconsole.log blocks the event loop preventing exit
BSIGINT event is not emitted on Ctrl+C
Cserver.close() is asynchronous; process.exit() is missing so process stays alive
Dapp.listen() must be called inside SIGINT handler
Attempts:
2 left
💡 Hint
Think about what happens after server.close() and how the process knows to exit.
state_output
advanced
2:00remaining
What is the value of 'connections' after this code runs?
This Express server tracks open connections to close them on shutdown. What is the value of the 'connections' Set after the code below runs and a client connects then disconnects?
Express
const express = require('express');
const app = express();
const server = app.listen(3000);
const connections = new Set();

server.on('connection', (conn) => {
  connections.add(conn);
  conn.on('close', () => connections.delete(conn));
});

// Simulate client connect and disconnect
const net = require('net');
const client = net.createConnection({ port: 3000 }, () => {
  client.end();
});
Aundefined
BSet(0) {} (empty set)
CSet(1) { <Socket> } (one connection still present)
Dnull
Attempts:
2 left
💡 Hint
Think about when the 'close' event fires and what happens to the Set.
🧠 Conceptual
expert
2:00remaining
Why is it important to track open connections during graceful shutdown in Express?
In Express, when shutting down gracefully, why should you track and close open connections before exiting the process?
ATo prevent clients from hanging by ensuring all requests finish before exit
BTo speed up server startup on next launch
CTo automatically restart the server without manual intervention
DTo free up memory used by the Express app immediately
Attempts:
2 left
💡 Hint
Think about user experience and network connections during shutdown.