Gathering detailed insights and metrics for http-graceful-shutdown
Gathering detailed insights and metrics for http-graceful-shutdown
Gathering detailed insights and metrics for http-graceful-shutdown
Gathering detailed insights and metrics for http-graceful-shutdown
http-shutdown
Gracefully shutdown a running HTTP server.
@moebius/http-graceful-shutdown
Gracefully terminates HTTP servers in Node.js
@types/http-graceful-shutdown
Stub TypeScript definitions entry for http-graceful-shutdown, which provides its own types definitions
http-server-shutdown
Nodejs server graceful shutdown
Gracefully shuts down node http server - can be used with http, express, koa, ...
npm install http-graceful-shutdown
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.3
Supply Chain
99.5
Quality
79.7
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
25,109,308
Last Day
23,820
Last Week
118,643
Last Month
511,106
Last Year
6,027,639
MIT License
220 Stars
105 Commits
18 Forks
5 Watchers
1 Branches
1 Contributors
Updated on Feb 18, 2025
Minified
Minified + Gzipped
Latest Version
3.1.14
Package Id
http-graceful-shutdown@3.1.14
Unpacked Size
32.01 kB
Size
8.82 kB
File Count
5
NPM Version
10.9.0
Node Version
22.12.0
Published on
Jan 03, 2025
Cumulative downloads
Total Downloads
Last Day
4%
23,820
Compared to previous day
Last Week
0%
118,643
Compared to previous week
Last Month
14%
511,106
Compared to previous month
Last Year
-19%
6,027,639
Compared to previous year
1
_ _ _ __ _ _ _ _
| |_| |_| |_ _ __ ___ __ _ _ _ __ _ __ ___ / _|_ _| |___ __| |_ _ _| |_ __| |_____ __ ___ _
| ' \ _| _| '_ \___/ _` | '_/ _` / _/ -_) _| || | |___(_-< ' \ || | _/ _` / _ \ V V / ' \
|_||_\__|\__| .__/ \__, |_| \__,_\__\___|_| \_,_|_| /__/_||_\_,_|\__\__,_\___/\_/\_/|_||_|
|_| |___/
Gracefully shuts down node.js http server. More than 10 Mio downloads overall.
Version 3.0 just released. This version is fully backwards compatible to version 2.x but adds much better handling under the hood. More that 10 Mio downloads.
http-graceful-shutdown
manages a secure and save shutdown of your http server
application:
1$ npm install http-graceful-shutdown
1const gracefulShutdown = require('http-graceful-shutdown'); 2... 3// app: can be http, https, express, koa, fastity, ... 4server = app.listen(...); 5... 6 7// this enables the graceful shutdown 8gracefulShutdown(server);
PARENT Process (e.g. nodemon, shell, kubernetes, ...)
─────────────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────
│ Signal (SIGINT, SIGTERM, ...)
│
│
(1) (2) v NODE SERVER (HTTP, Express, koa, fastity, ...)
▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇
│ │ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ │ <─ shutdown procedure
│ │ shutdown initiated │ │ │
│ │ │ │ │
│ │ │ │ (8) shutdown function (9) finally fn │
│ │ ▄▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄ │ │ ▄▄ ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄▄▄▄ │
│ └ (3) (4) close │ └ (7) destroy │
│ preShutdown idle sockets │ remaining sockets │
│ │ │ (10)
serve │ serving req. (open connection) │ (5) └ SERVER terminated
▄▄▄│ ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄┤ ^ blocked
^ │ ^ last request before │ │
│ │ │ receiving shutdown signal │ │
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
│ │ │ Long request │ │
Request │ V Resp │ V Resp. │
│ │ │ CLIENT
────────┴─────────┴─────────────────────────────────────────────────┴─────────────────────────────────────────────────────────
option | default | Comments |
---|---|---|
timeout | 30000 | timeout till forced shutdown (in milliseconds) |
signals | 'SIGINT SIGTERM' | define the signals, that should be handled (separated by SPACE) |
development | false | if set to true, no graceful shutdown is proceeded to speed up dev-process |
preShutdown | - | not time-consuming callback function. Needs to return a promise. Here, all HTTP sockets are still available and untouched |
onShutdown | - | not time-consuming callback function. Needs to return a promise. |
forceExit | true | force process.exit - otherwise just let event loop clear |
finally | - | small, not time-consuming function, that will be handled at the end of the shutdown (not in dev-mode) |
You can pass an options-object to specify your specific options for the graceful shutdown
The following example uses all possible options:
1const gracefulShutdown = require('http-graceful-shutdown'); 2... 3// app: can be http, https, express, koa, fastity, ... 4server = app.listen(...); 5... 6 7// your personal cleanup function 8// - must return a promise 9// - the input parameter is optional (only needed if you want to 10// access the signal type inside this function) 11// - this function here in this example takes one second to complete 12function shutdownFunction(signal) { 13 return new Promise((resolve) => { 14 console.log('... called signal: ' + signal); 15 console.log('... in cleanup') 16 setTimeout(function() { 17 console.log('... cleanup finished'); 18 resolve(); 19 }, 1000) 20 }); 21} 22 23// finally function 24// -- sync function 25// -- should be very short (not time consuming) 26function finalFunction() { 27 console.log('Server gracefulls shutted down.....') 28} 29 30// this enables the graceful shutdown with advanced options 31gracefulShutdown(server, 32 { 33 signals: 'SIGINT SIGTERM', 34 timeout: 10000, // timeout: 10 secs 35 development: false, // not in dev mode 36 forceExit: true, // triggers process.exit() at the end of shutdown process 37 preShutdown: preShutdownFunction, // needed operation before httpConnections are shutted down 38 onShutdown: shutdownFunction, // shutdown function (async) - e.g. for cleanup DB, ... 39 finally: finalFunction // finally function (sync) - e.g. for logging 40 } 41);
You can now trigger gracefulShutdown programatically (e.g. for tests) like so:
1let shutdown 2beforeAll(() => { 3 shutdown = gracefulShutdown(...) 4}) 5 6afterAll(async () => { 7 await shutdown() 8})
With the forceExit
option, you can define how your node server process ends:
when setting forceExit
to false
, you just let the event loop clear and then
the proccess ends automatically:
1const gracefulShutdown = require('http-graceful-shutdown'); 2... 3// app: can be http, https, express, koa, fastity, ... 4server = app.listen(...); 5... 6 7// enable graceful shutdown with options: 8// this option lets the event loop clear to end your node server 9// no explicit process.exit() will be triggered. 10 11gracefulShutdown(server, { 12 forceExit: false 13});
If you want an explicit process.exit() at the end, set forceExit
to true
(which is the default).
If you want to get debug notes (debug is a dependency of this module), just set the DEBUG environment variable to enable debugging:
export DEBUG=http-graceful-shutdown
OR on Windows:
set DEBUG=http-graceful-shutdown
You can find examples how to use http-graceful-shutdown
with Express, Koa,
http, http2, fastify in the examples
directory. To run the examples, be sure
to install debug and express, koa or fastify.
npm install debug express koa fastify
Version | Date | Comment |
---|---|---|
3.1.14 | 2025-01-03 | updated docs |
3.1.13 | 2023-02-11 | fix forceExit default value |
3.1.12 | 2022-12-04 | changed lgtm to github scanning |
3.1.11 | 2022-11-18 | updated examples |
3.1.10 | 2022-11-17 | forceExit handling adapted |
3.1.9 | 2022-10-24 | updated docs, code cleanup |
3.1.8 | 2022-07-27 | updated docs, fixed typos |
3.1.7 | 2022-03-18 | updated dependencies, updated docs |
3.1.6 | 2022-02-27 | updated dependencies |
3.1.5 | 2021-11-08 | updated docs |
3.1.4 | 2021-08-27 | updated docs |
3.1.3 | 2021-08-03 | fixed handle events once (thanks to Igor Basov) |
3.1.2 | 2021-06-15 | fixed cleanupHttp() no timeout |
3.1.1 | 2021-05-13 | updated docs |
3.1.0 | 2021-05-08 | refactoring, added preShutdown |
3.0.2 | 2021-04-08 | updated docs |
3.0.1 | 2021-02-26 | code cleanup |
3.0.0 | 2021-02-25 | version 3.0 release |
2.4.0 | 2021-02-15 | added forceExit option (defaults to true) |
2.3.2 | 2019-06-14 | typescript typings fix |
2.3.1 | 2019-05-31 | updated docs, added typescript typings |
2.3.0 | 2019-05-30 | added manual shutdown (for tests) see docs below |
2.2.3 | 2019-02-01 | updated docs, debug |
2.2.2 | 2018-12-28 | updated docs, keywords |
2.2.1 | 2018-11-20 | updated docs |
2.2.0 | 2018-11-19 | added (optional) signal type to shutdown function - see example |
2.1.3 | 2018-11-06 | updated docs |
2.1.2 | 2018-11-03 | updated dependencies (version bump), updated docs |
2.1.1 | 2018-02-28 | extended isFunction to support e.g. AsyncFunctions |
2.1.0 | 2018-02-11 | bug fixing onShutdown method was called before server.close |
2.0.6 | 2017-11-06 | updated docs, code cleanup |
2.0.5 | 2017-11-06 | updated dependencies, modifications gitignore, added docs |
2.0.4 | 2017-09-21 | updated dependencies, modifications gitignore |
2.0.3 | 2017-06-18 | updated dependencies |
2.0.2 | 2017-05-27 | fixed return value 0 |
2.0.1 | 2017-04-24 | modified documentation |
2.0.0 | 2017-04-24 | added 'onShutdown' option, renamed 'callback' option to 'finally' |
1.0.6 | 2016-02-03 | adding more explicit debug information and documentation |
1.0.5 | 2016-02-01 | better handling of closing connections |
1.0.4 | 2015-10-01 | small fixes |
1.0.3 | 2015-09-15 | updated docs |
1.0.1 | 2015-09-14 | updated docs, reformated code |
1.0.0 | 2015-09-14 | initial release |
If you have ideas, comments or questions, please do not hesitate to contact me.
Sincerely,
Sebastian Hildebrandt, +innovations
Written by Sebastian Hildebrandt sebhildebrandt
The
MIT
License (MIT)Copyright © 2015-2025 Sebastian Hildebrandt, +innovations.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
SAST tool detected: CodeQL
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
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