https://github.com/someimportantcompany/koa-graceful-shutdown
Ensure that during shutdown Koa returns correctly with a 503
https://github.com/someimportantcompany/koa-graceful-shutdown
graceful-shutdown koa2 nodejs
Last synced: 3 months ago
JSON representation
Ensure that during shutdown Koa returns correctly with a 503
- Host: GitHub
- URL: https://github.com/someimportantcompany/koa-graceful-shutdown
- Owner: someimportantcompany
- Created: 2018-07-27T18:13:53.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T06:27:58.000Z (almost 3 years ago)
- Last Synced: 2025-06-27T08:54:10.939Z (4 months ago)
- Topics: graceful-shutdown, koa2, nodejs
- Language: JavaScript
- Homepage: https://npm.im/koa-graceful-shutdown
- Size: 518 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# koa-graceful-shutdown
[](https://npm.im/koa-graceful-shutdown)
[](https://github.com/someimportantcompany/koa-graceful-shutdown/actions/workflows/ci.yml)
[](https://coveralls.io/github/someimportantcompany/koa-graceful-shutdown)Ensure that during shutdown [Koa](https://github.com/koajs/koa) returns correctly with a `HTTP 503 Service Unavailable`. Based off [`express-graceful-shutdown`](https://github.com/serby/express-graceful-shutdown) with the middleware adapted for Koa.
```js
const http = require('http');
const Koa = require('koa');
const shutdown = require('koa-graceful-shutdown');const app = new Koa();
const server = http.createServer(app.callback());app.use(shutdown(server));
app.use(ctx => {
ctx.status = 200;
ctx.body = { foo: 'bar' };
});server.listen(0, 'localhost', () => {
const { address, port } = server.address();
console.log('Listening on http://%s:%d', address, port);
});
```## Install
```
npm install koa-graceful-shutdown --save
```## Arguments
```
shutdown(server, opts) => function(ctx, next)
```Argument | Description
---- | ----
`server` | [`http.server`](https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_class_http_server)
`opts` | Optional options
`opts.logger` | A logger that provides `info`, `warn` and `error` methods, defaults to `console`
`opts.forceTimeout` | Milliseconds to wait for `server.close()` to finish, defaults to `30000`## Notes
- Original credits to [Paul Serby](https://github.com/serby/) for [`express-graceful-shutdown`](https://github.com/serby/express-graceful-shutdown).
- Any questions or suggestions please [open an issue](https://github.com/someimportantcompany/koa-graceful-shutdown/issues).