What is server-destroy?
The server-destroy npm package provides a simple way to add a destroy method to a Node.js HTTP server, allowing for the immediate termination of all active connections. This is particularly useful for gracefully shutting down servers during development or in production environments.
Add destroy method to server
This feature allows you to add a destroy method to a Node.js HTTP server. The code sample demonstrates how to create an HTTP server, enable the destroy method, and then destroy the server after 5 seconds.
const http = require('http');
const enableDestroy = require('server-destroy');
const server = http.createServer((req, res) => {
res.end('Hello, world!');
});
// Enable the destroy method on the server
enableDestroy(server);
server.listen(3000, () => {
console.log('Server is listening on port 3000');
});
// Destroy the server after 5 seconds
setTimeout(() => {
server.destroy(() => {
console.log('Server destroyed');
});
}, 5000);