HTTP Shutdown

1.2.2 · maintenance · verified Sun Apr 19

http-shutdown is a Node.js library designed for gracefully shutting down HTTP servers, addressing shortcomings of the native `server.close()` method. Unlike `server.close()` which only terminates the listening socket and waits for existing connections, `http-shutdown` actively closes idle keep-alive sockets and waits for all in-flight requests to complete before closing their associated sockets. This ensures no new requests are accepted and all current requests finish cleanly. The library, currently at version 1.2.2, appears to be in a maintenance phase with infrequent but stable releases. It offers two primary integration methods: explicitly wrapping an `http.Server` instance or extending the `http.Server` prototype with a `withShutdown()` method. It ships with TypeScript types, facilitating its use in modern TypeScript projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to explicitly wrap an `http.Server` instance with `http-shutdown` for graceful termination. This example includes simulating a long-running request, initiating shutdown after a delay, and correctly handling common OS termination signals (`SIGINT`, `SIGTERM`) to ensure all connections are closed cleanly.

import * as http from 'http';
import httpShutdown, { WithShutdownServer } from 'http-shutdown';

// Create the http server
let server: http.Server | WithShutdownServer = http.createServer((req, res) => {
  if (req.url === '/heavy-task') {
    // Simulate a long-running request
    console.log('Incoming heavy task request...');
    setTimeout(() => {
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.end('Heavy task completed!');
      console.log('Heavy task finished.');
    }, 2500);
  } else {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello from http-shutdown!');
  }
});

// Wrap the server object with additional functionality.
server = httpShutdown(server);

const PORT = 3000;

// Listen on a port and start taking requests.
server.listen(PORT, () => {
  console.log(`Server listening on http://localhost:${PORT}`);
  console.log('Try accessing / and /heavy-task. Server will attempt graceful shutdown in 7 seconds...');

  // Sometime later... initiate graceful shutdown.
  setTimeout(() => {
    console.log('Initiating graceful shutdown...');
    (server as WithShutdownServer).shutdown((err) => {
      if (err) {
        console.error('Shutdown failed:', err.message);
        process.exit(1);
        return;
      }
      console.log('Everything is cleanly shutdown.');
      process.exit(0);
    });
  }, 7000);
});

// Handle common process termination signals for robust shutdown
process.on('SIGTERM', () => {
  console.log('SIGTERM received, initiating shutdown.');
  (server as WithShutdownServer).shutdown((err) => {
    if (err) {
      console.error('Shutdown failed on SIGTERM:', err.message);
      process.exit(1);
      return;
    }
    console.log('Everything is cleanly shutdown after SIGTERM.');
    process.exit(0);
  });
});

process.on('SIGINT', () => {
  console.log('SIGINT received, initiating shutdown.');
  (server as WithShutdownServer).shutdown((err) => {
    if (err) {
      console.error('Shutdown failed on SIGINT:', err.message);
      process.exit(1);
      return;
    }
    console.log('Everything is cleanly shutdown after SIGINT.');
    process.exit(0);
  });
});

view raw JSON →