0
0
Expressframework~10 mins

Graceful shutdown handling in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Express library.

Express
const express = require([1]);
Drag options to blanks, or click blank then click option'
A"http"
B"express"
C"fs"
D"path"
Attempts:
3 left
💡 Hint
Common Mistakes
Using other module names like "http" or "fs" instead of "express".
2fill in blank
medium

Complete 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'
A8080
B5000
C3000
D80
Attempts:
3 left
💡 Hint
Common Mistakes
Using ports like 80 or 5000 which are valid but not the example's port.
3fill in blank
hard

Fix 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'
Aclose
Bstop
Cshutdown
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using stop() or shutdown() instead of close().
4fill in blank
hard

Fill 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'
ASIGTERM
BSIGINT
Cclose
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing SIGINT with SIGTERM.
Using stop() instead of close().
5fill in blank
hard

Fill 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'
ASIGINT
Bclose
Csignal
DSIGQUIT
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.