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.
npm install http-shutdownVerified import paths — ran on the pinned version, not inferred.
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.
Thoroughly test `http-shutdown` with your `https.Server` setup. The core logic typically applies, but any HTTPS-specific connection management or protocol nuances might require extra consideration.
Choose one integration method: either explicitly wrap each `http.Server` instance (`server = httpShutdown(server);`) or use `require('http-shutdown').extend();` once to augment the `http.Server.prototype` globally, then call `.withShutdown()` on your server instances. Do not do both for the same server or in the same application.Ensure the `http.Server` is in a listening state before invoking `shutdown()`. While the library generally handles multiple calls gracefully, it's best practice to design your application's shutdown logic to trigger `shutdown()` only once, typically in response to process signals like `SIGINT` or `SIGTERM`.
For ESM environments, use the default import syntax: `import httpShutdown from 'http-shutdown';`. If you need to access `.extend()`, call it from the default import: `httpShutdown.extend();`. Ensure your `tsconfig.json` or bundler settings (e.g., `esModuleInterop`) are configured correctly for interoperability.
Ensure you have either explicitly wrapped your server with `server = httpShutdown(server);` or called `require('http-shutdown').extend();` and subsequently `server = http.createServer(...).withShutdown();` before attempting to call `server.shutdown()`.Make sure you call `require('http-shutdown').extend();` (or `httpShutdown.extend();` for ESM) once at the application's entry point, and then chain `.withShutdown()` directly after creating your server: `server = http.createServer(...).withShutdown();`.Always rely exclusively on the callback provided to `httpShutdown.shutdown(callback)` for knowing when the server has gracefully and completely shut down. Place all post-shutdown cleanup logic within this callback.
No dependency data recorded yet.