{"library":"stoppable","title":"Stoppable HTTP/HTTPS Server Shutdown","type":"library","description":"Stoppable is a Node.js library that decorates standard `http.Server` and `https.Server` instances with a `stop()` method, addressing a long-standing behavior of Node.js's native `server.close()` that often leaves existing connections open indefinitely. This library ensures a graceful shutdown by stopping new connections, allowing in-flight requests to complete, and then closing existing idle connections, optionally with a grace period for force-closing. The current stable version is 1.1.0, last updated in 2017. While widely adopted and stable for its intended purpose, it is no longer actively maintained. Its key differentiator is providing a reliable, explicit mechanism for graceful server shutdown that was historically missing from Node.js core, with minimal performance overhead. Alternatives like `http-terminator` offer similar functionality with different design choices, often avoiding monkey-patching.","language":"javascript","status":"maintenance","last_verified":"Sun Apr 19","install":{"commands":["npm install stoppable"],"cli":null},"imports":["const stoppable = require('stoppable');","import stoppable from 'stoppable';"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/hunterloftis/stoppable","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/stoppable","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import http from 'http';\nimport stoppable from 'stoppable';\n\nconst handler = (req, res) => {\n  console.log(`Request received for: ${req.url}`);\n  setTimeout(() => {\n    res.writeHead(200, { 'Content-Type': 'text/plain' });\n    res.end('Hello from stoppable server!\\n');\n  }, 500); // Simulate some work\n};\n\n// Create a standard HTTP server and make it stoppable with a 2-second grace period.\nconst server = stoppable(http.createServer(handler), 2000);\n\nserver.listen(3000, () => {\n  console.log('Server listening on http://localhost:3000');\n  console.log('Press Ctrl+C to initiate graceful shutdown...');\n});\n\nprocess.on('SIGTERM', () => {\n  console.log('\\nSIGTERM received. Initiating graceful shutdown...');\n  server.stop((err, gracefully) => {\n    if (err) {\n      console.error('Server failed to stop:', err);\n      process.exit(1);\n    } else if (gracefully) {\n      console.log('Server stopped gracefully.');\n      process.exit(0);\n    } else {\n      console.warn('Server stopped, but not all connections were closed gracefully.');\n      process.exit(0);\n    }\n  });\n});\n\nprocess.on('SIGINT', () => {\n  console.log('\\nSIGINT received. Initiating graceful shutdown...');\n  server.stop((err, gracefully) => {\n    if (err) {\n      console.error('Server failed to stop:', err);\n      process.exit(1);\n    } else if (gracefully) {\n      console.log('Server stopped gracefully.');\n      process.exit(0);\n    } else {\n      console.warn('Server stopped, but not all connections were closed gracefully.');\n      process.exit(0);\n    }\n  });\n});","lang":"javascript","description":"This quickstart demonstrates how to create an HTTP server, make it stoppable with a grace period, and handle `SIGTERM`/`SIGINT` signals for graceful shutdown, allowing in-flight requests to complete.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}