Server Destroy

1.0.1 · abandoned · verified Sun Apr 19

server-destroy is a lightweight Node.js utility designed to enhance `http.Server` or `net.Server` instances with a `destroy()` method. This method allows for the graceful shutdown of a server by not only stopping it from accepting new connections but also forcibly closing all existing open connections, a feature not natively present in older Node.js versions. The package is stable at version 1.0.1 but has not seen updates since its last publish in October 2015, making it effectively abandoned. Its release cadence is non-existent. Key differentiators at the time of its release were providing a complete server shutdown mechanism beyond `server.close()`, which only stops listening for new connections. With Node.js v18.2.0 and later, similar functionality is available natively via `server.closeAllConnections()`. Therefore, this package is primarily useful for projects targeting older Node.js runtimes.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to create an HTTP server, enhance it with the `destroy` method using `server-destroy`, and then gracefully shut it down, closing all active connections after 5 seconds. It also shows basic signal handling.

const http = require('http');
const enableDestroy = require('server-destroy');

const PORT = process.env.PORT || 3000;

const server = http.createServer((req, res) => {
  console.log(`Request received for ${req.url}`);
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello from server-destroy demo!');
});

// Enhance the server with a 'destroy' function
enableDestroy(server);

server.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});

// Simulate a shutdown after a few seconds
setTimeout(() => {
  console.log('Initiating server shutdown...');
  server.destroy((err) => {
    if (err) {
      console.error('Error during server destruction:', err);
    } else {
      console.log('Server and all connections destroyed successfully.');
      process.exit(0);
    }
  });
}, 5000);

// Handle process exit signals to ensure clean shutdown
process.on('SIGTERM', () => server.destroy(() => process.exit(0)));
process.on('SIGINT', () => server.destroy(() => process.exit(0)));

view raw JSON →