Server with Kill Method
This package provides a utility to augment Node.js `http.Server` instances with a `kill` method that forcibly destroys all active connections. This functionality is particularly useful in development environments or during testing where immediate server shutdown is required without waiting for connections to naturally close. The current stable version is 1.0.0, released in October 2019. Given the age of the last release and lack of further updates, it can be considered abandoned. It differentiates itself by offering a simple, direct way to manage server connections beyond the standard `server.close()` which waits for active connections to finish. Its primary use case is to ensure a clean slate for server processes, especially in automated test suites.
Common errors
-
TypeError: server.kill is not a function
cause Attempting to call `.kill()` directly on a standard `http.Server` instance without first transforming it with `server-with-kill`.fixEnsure you pass your `http.Server` instance through the `transform` function: `const serverWithKill = transform(yourHttpServer);` -
Error: read ECONNRESET
cause A client received a 'connection reset' error because the server forcibly closed the connection using `server.kill()` while the client was still active.fixThis is an expected outcome when using `server.kill()`. It signifies that the connections were indeed destroyed. It's a 'problem' for the client, not necessarily the server. Inform clients that the server might abruptly shut down, or design your architecture to handle sudden disconnections.
Warnings
- breaking The package has not been updated since its initial release in October 2019 (v1.0.0). It may not be fully compatible with newer Node.js versions or could have unresolved security vulnerabilities related to Node's internal `http` module changes. Relying on an unmaintained package for production is risky.
- gotcha Forcibly destroying connections with `server.kill()` can lead to unexpected client behavior, data loss, or corrupted states if clients are in the middle of a request/response cycle. It's an immediate, blunt instrument.
- gotcha The `kill` method accepts an optional callback, but it's important to handle errors that might occur during the destruction of sockets. While less common, socket destruction can sometimes fail.
Install
-
npm install server-with-kill -
yarn add server-with-kill -
pnpm add server-with-kill
Imports
- transform
import serverWithKill from 'server-with-kill';
import { transform } from 'server-with-kill'; - transform
const transform = require('server-with-kill');const { transform } = require('server-with-kill'); - ServerWithKill
import { ServerWithKill } from 'server-with-kill/dist/types/server-with-kill.d.ts'; // For type-only imports, though typically inferred
Quickstart
import express from 'express';
import { transform } from 'server-with-kill';
import { Server } from 'http';
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.send('Hello from server-with-kill! This server will be killed soon.');
});
app.get('/long-task', (req, res) => {
console.log('Received request for long-task...');
// Simulate a long-running process that might be interrupted by kill
setTimeout(() => {
res.send('Long task completed (if not killed)!');
}, 5000);
});
const httpServer: Server = app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});
const serverWithKill = transform(httpServer);
console.log('Server started. It will be forcibly killed in 3 seconds.');
setTimeout(() => {
serverWithKill.kill((err) => {
if (err) {
console.error('Error during server kill:', err.message);
} else {
console.log('Server killed and all active connections destroyed.');
}
process.exit(0);
});
}, 3000);