Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Express library.
Express
const express = require([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using other module names like "http" or "fs" instead of "express".
✗ Incorrect
The Express library is imported using require("express") to create an app.
2fill in blank
mediumComplete the code to start the server listening on port 3000.
Express
const server = app.listen([1], () => { console.log('Server running'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ports like 80 or 5000 which are valid but not the example's port.
✗ Incorrect
Port 3000 is commonly used for local Express servers.
3fill in blank
hardFix the error in the shutdown handler to close the server properly.
Express
process.on('SIGINT', () => { console.log('Shutting down...'); server.[1](() => { console.log('Server closed'); process.exit(0); }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using stop() or shutdown() instead of close().
✗ Incorrect
The Express server instance has a close() method to stop accepting new connections.
4fill in blank
hardFill both blanks to handle server shutdown on SIGTERM signal.
Express
process.on('[1]', () => { console.log('SIGTERM received'); server.[2](() => { console.log('Server closed on SIGTERM'); process.exit(0); }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing SIGINT with SIGTERM.
Using stop() instead of close().
✗ Incorrect
SIGTERM is a termination signal. The server closes with close() method.
5fill in blank
hardFill all three blanks to log shutdown reason and close the server.
Express
function gracefulShutdown(signal) {
console.log(`Received $[1] signal.`);
server.[2](() => {
console.log(`Server closed due to $[3]`);
process.exit(0);
});
}
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
process.on('SIGTERM', () => gracefulShutdown('SIGTERM')); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using SIGQUIT which is not handled here.
Using stop() instead of close().
Using a fixed string instead of the variable signal.
✗ Incorrect
The function logs the signal name, closes the server with close(), and logs the signal reason.