Performance: Graceful shutdown on errors
This affects server responsiveness and resource cleanup during error conditions, impacting user experience and server stability.
Jump into concepts and practice - no test required
process.on('uncaughtException', (err) => { console.error('Uncaught Exception:', err); server.close(() => { // cleanup resources here process.exit(1); }); });
process.on('uncaughtException', (err) => { console.error('Uncaught Exception:', err); // No shutdown or cleanup, server keeps running });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No graceful shutdown on error | N/A | N/A | N/A | [X] Bad |
| Graceful shutdown with cleanup | N/A | N/A | N/A | [OK] Good |
const server = require('http').createServer();
process.on('SIGINT', () => {
console.log('Shutdown signal received');
server.close(() => {
console.log('Server closed');
process.exit(0);
});
});
server.listen(3000, () => console.log('Server running'));
What will be the output if you press Ctrl+C in the terminal?const server = require('http').createServer();
process.on('uncaughtException', (err) => {
console.error('Error:', err);
process.exit(1);
});
server.listen(3000);SIGTERM and uncaughtException events, ensuring the server closes before exit. Which code snippet correctly achieves this?