{"id":18942357,"url":"https://github.com/thedillonb/http-shutdown","last_synced_at":"2025-04-13T02:26:12.317Z","repository":{"id":57096804,"uuid":"46842096","full_name":"thedillonb/http-shutdown","owner":"thedillonb","description":"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.","archived":false,"fork":false,"pushed_at":"2020-01-10T13:26:19.000Z","size":21,"stargazers_count":177,"open_issues_count":6,"forks_count":21,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-04T04:35:22.407Z","etag":null,"topics":["nodejs","shutdown","socket"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thedillonb.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-25T06:28:55.000Z","updated_at":"2025-04-04T03:31:26.000Z","dependencies_parsed_at":"2022-08-20T16:11:04.128Z","dependency_job_id":null,"html_url":"https://github.com/thedillonb/http-shutdown","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedillonb%2Fhttp-shutdown","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedillonb%2Fhttp-shutdown/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedillonb%2Fhttp-shutdown/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedillonb%2Fhttp-shutdown/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thedillonb","download_url":"https://codeload.github.com/thedillonb/http-shutdown/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248656283,"owners_count":21140673,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nodejs","shutdown","socket"],"created_at":"2024-11-08T12:32:34.912Z","updated_at":"2025-04-13T02:26:12.298Z","avatar_url":"https://github.com/thedillonb.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Http-Shutdown [![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url] [![Test coverage][coveralls-image]][coveralls-url]\nShutdown a Nodejs HTTP server gracefully by doing the following:\n\n1. Close the listening socket to prevent new connections\n2. Close all idle keep-alive sockets to prevent new requests during shutdown\n3. Wait for all in-flight requests to finish before closing their sockets.\n4. Profit!\n\nOther 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.\n\n`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.\n\n## Installation\n\n```bash\n$ npm install http-shutdown\n```\n\n## Usage\nThere are currently two ways to use this library. The first is explicit wrapping of the `Server` object:\n\n```javascript\n// Create the http server\nvar server = require('http').createServer(function(req, res) {\n  res.end('Good job!');\n});\n\n// Wrap the server object with additional functionality.\n// This should be done immediately after server construction, or before you start listening.\n// Additional functionailiy needs to be added for http server events to properly shutdown.\nserver = require('http-shutdown')(server);\n\n// Listen on a port and start taking requests.\nserver.listen(3000);\n\n// Sometime later... shutdown the server.\nserver.shutdown(function(err) {\n\tif (err) {\n\t\treturn console.log('shutdown failed', err.message);\n\t}\n\tconsole.log('Everything is cleanly shutdown.');\n});\n```\n\nThe second is implicitly adding prototype functionality to the `Server` object:\n\n```javascript\n// .extend adds a .withShutdown prototype method to the Server object\nrequire('http-shutdown').extend();\n\nvar server = require('http').createServer(function(req, res) {\n  res.end('God job!');\n}).withShutdown(); // \u003c-- Easy to chain. Returns the Server object\n\n// Sometime later, shutdown the server.\nserver.shutdown(function(err) {\n\tif (err) {\n\t\treturn console.log('shutdown failed', err.message);\n\t}\n  console.log('Everything is cleanly shutdown.');\n});\n```\n\n## Test\n```bash\n$ npm test\n```\n\n[npm-image]: https://img.shields.io/npm/v/http-shutdown.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/http-shutdown\n[travis-image]: https://img.shields.io/travis/thedillonb/http-shutdown.svg?style=flat-square\n[travis-url]: https://travis-ci.org/thedillonb/http-shutdown\n[coveralls-image]: https://img.shields.io/coveralls/thedillonb/http-shutdown.svg?style=flat-square\n[coveralls-url]: https://coveralls.io/r/thedillonb/http-shutdown\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthedillonb%2Fhttp-shutdown","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthedillonb%2Fhttp-shutdown","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthedillonb%2Fhttp-shutdown/lists"}