1.0.0 • Published 6 years ago

@tastespuds/koa-shutdown v1.0.0

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

koa-shutdown

Ensure that during shutdown Koa returns correctly with a HTTP 503 Service Unavailable. Based off express-graceful-shutdown with the middleware adapted for Koa.

const shutdown = require('@tastespuds/koa-shutdown');
const http = require('http');
const Koa = require('koa');

const app = new Koa();
const server = http.createServer(app.callback());

app.use(shutdown.createShutdownMiddleware(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 @tastespuds/koa-shutdown --save

Module Properties

shutdown.createShutdownMiddleware(server, options) => async function middleware(ctx, next)
ArgumentDescription
serverhttp.server
optionsOptional options
options.loggerA logger that provides info, warn and error methods, defaults to console
options.onShutdownAsync callback for other connections that need closing (e.g. database)
options.forceTimeoutMilliseconds to wait for server.close() to finish, defaults to 30000
options.additionalSignalsAllows you to add more signals to close on