Server with Kill Method

1.0.0 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates starting an Express server, wrapping it with `server-with-kill`'s `transform` function, and then forcibly shutting it down after a set delay, including a simulated long task.

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);

view raw JSON →