Shutdown a Nodejs HTTP server gracefully by terminating the listening socket, then destroying all keep-alive idle sockets all while allowing in-flight requests to finish.
Installations
npm install http-shutdown
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
>= 0.12.0
Node Version
10.15.3
NPM Version
6.9.0
Score
99.3
Supply Chain
90
Quality
75.4
Maintenance
100
Vulnerability
100
License
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
validate.email 🚀
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Developer
thedillonb
Download Statistics
Total Downloads
177,590,816
Last Day
226,035
Last Week
1,148,132
Last Month
5,030,580
Last Year
57,265,039
GitHub Statistics
MIT License
175 Stars
41 Commits
21 Forks
12 Watchers
1 Branches
8 Contributors
Updated on Dec 05, 2024
Package Meta Information
Latest Version
1.2.2
Package Id
http-shutdown@1.2.2
Size
4.15 kB
NPM Version
6.9.0
Node Version
10.15.3
Published on
Jan 10, 2020
Total Downloads
Cumulative downloads
Total Downloads
177,590,816
Last Day
0.4%
226,035
Compared to previous day
Last Week
-0.6%
1,148,132
Compared to previous week
Last Month
20.5%
5,030,580
Compared to previous month
Last Year
-30.7%
57,265,039
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
5
Http-Shutdown

Shutdown a Nodejs HTTP server gracefully by doing the following:
- Close the listening socket to prevent new connections
- Close all idle keep-alive sockets to prevent new requests during shutdown
- Wait for all in-flight requests to finish before closing their sockets.
- Profit!
Other solutions might just use server.close
which only terminates the listening socket and waits for other sockets to close - which is incomplete since keep-alive sockets can still make requests. Or, they may use ref()/unref()
to simply cause Nodejs to terminate if the sockets are idle - which doesn't help if you have other things to shutdown after the server shutsdown.
http-shutdown
is a complete solution. It uses idle indicators combined with an active socket list to safely, and gracefully, close all sockets. It does not use ref()/unref()
but, instead, actively closes connections as they finish meaning that socket 'close' events still work correctly since the sockets are actually closing - you're not just unref
ing and forgetting about them.
Installation
1$ npm install http-shutdown
Usage
There are currently two ways to use this library. The first is explicit wrapping of the Server
object:
1// Create the http server 2var server = require('http').createServer(function(req, res) { 3 res.end('Good job!'); 4}); 5 6// Wrap the server object with additional functionality. 7// This should be done immediately after server construction, or before you start listening. 8// Additional functionailiy needs to be added for http server events to properly shutdown. 9server = require('http-shutdown')(server); 10 11// Listen on a port and start taking requests. 12server.listen(3000); 13 14// Sometime later... shutdown the server. 15server.shutdown(function(err) { 16 if (err) { 17 return console.log('shutdown failed', err.message); 18 } 19 console.log('Everything is cleanly shutdown.'); 20});
The second is implicitly adding prototype functionality to the Server
object:
1// .extend adds a .withShutdown prototype method to the Server object 2require('http-shutdown').extend(); 3 4var server = require('http').createServer(function(req, res) { 5 res.end('God job!'); 6}).withShutdown(); // <-- Easy to chain. Returns the Server object 7 8// Sometime later, shutdown the server. 9server.shutdown(function(err) { 10 if (err) { 11 return console.log('shutdown failed', err.message); 12 } 13 console.log('Everything is cleanly shutdown.'); 14});
Test
1$ npm test

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 7/20 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 19 are checked with a SAST tool
Score
3.4
/10
Last Scanned on 2025-02-17
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More