http-terminator provides a robust solution for gracefully shutting down Node.js HTTP(S) servers. Unlike the native `server.close()` method, which simply stops accepting new connections but leaves existing connections open indefinitely (potentially hanging due to keep-alive or long-running requests), this library actively tracks and terminates all connections after a configurable timeout. It ensures that in-flight requests complete their responses and communicates shutdown intent to clients. The current stable version is `3.2.0`, with a release cadence that includes minor and patch updates every few months, reflecting ongoing maintenance and feature additions. Key differentiators include its explicit handling of all connection types, including `http`, `https`, and `http2` servers (since v3.2.0), and its rewrite to TypeScript in v3.0.0, providing robust type definitions.
npm install http-terminatorVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to set up a basic HTTP server, integrate `http-terminator` for graceful shutdown, and respond to common OS signals (SIGTERM, SIGINT). It includes a simulated asynchronous request to showcase the termination timeout.
Update your project's type definitions to use TypeScript if upgrading from a pre-v3 version. Review any custom type extensions for compatibility.
Ensure your project is running on Node.js v14 or newer to use http-terminator v3.1.0 and above.
Always use `httpTerminator.terminate()` provided by this library for a proper graceful shutdown that handles existing connections and in-flight requests.
Adopt `import` statements for `http-terminator`. If your project is CommonJS, consider transpiling or using dynamic `import()`.
Configure `gracefulTerminationTimeout` to a value appropriate for your application's expected maximum request duration: `createHttpTerminator({ server, gracefulTerminationTimeout: 30000 })`.Change your import statement to `import { createHttpTerminator } from 'http-terminator';` and ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`).Implement graceful shutdown using `http-terminator.terminate()` in your `SIGTERM`/`SIGINT` handlers, ensuring `httpTerminator` is initialized with your `http.Server` instance.
Increase the `gracefulTerminationTimeout` option when calling `createHttpTerminator()` to allow more time for requests to finish, or investigate why requests are taking so long.
No dependency data recorded yet.