Server Destroy
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
-
ReferenceError: require is not defined in ES module scope
cause You are attempting to use the CommonJS `require()` function in an ECMAScript Module (ESM) file or project.fixEnsure your file is a CommonJS module (e.g., `.js` file without `"type": "module"` in `package.json`, or explicitly using `.cjs` extension). If you must use ESM, for Node.js v18.2.0+, consider using the native `server.closeAllConnections()`. -
Error: listen EADDRINUSE: address already in use :::<port_number>
cause A previous server instance or another process is still holding the port open, often because active connections were not properly closed during shutdown.fixEnsure your server's shutdown logic explicitly calls `server.destroy()` (or `server.closeAllConnections()` for newer Node.js versions) to terminate all open connections. For lingering processes, use `kill -9 <PID>` on Linux/macOS or Task Manager on Windows to manually terminate the process.
Warnings
- gotcha This package is CommonJS-only (`require()`). Attempting to use `import` syntax in an ES Module project will result in runtime errors. Ensure your project is configured for CommonJS or use a CJS wrapper if absolutely necessary.
- deprecated The `server-destroy` package has not been updated since October 2015 and is no longer actively maintained. While it may still function for its intended purpose, it will not receive bug fixes, security updates, or compatibility improvements for newer Node.js versions or evolving ecosystem standards.
- gotcha Node.js versions 18.2.0 and higher now include a native `server.closeAllConnections()` method, which offers similar functionality to `server-destroy` for closing all active connections. For modern applications, using the built-in method is generally preferred to reduce external dependencies and ensure compatibility with the Node.js core.
Install
-
npm install server-destroy -
yarn add server-destroy -
pnpm add server-destroy
Imports
- enableDestroy
import enableDestroy from 'server-destroy';
const enableDestroy = require('server-destroy'); - Server
/// <reference types="node" /> import * as net from 'net'; // ... enableDestroy(server as net.Server);
Quickstart
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)));