Skip to content

Neos1/koa-graceful-shutdown

 
 

Repository files navigation

koa-graceful-shutdown

NPM CircleCI

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 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
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

Examples

Before shutdown

const beforeShutdown = (exitCallback) => {
  // DO SOMETHING...
  exitCallback();
};

app.use(shutdown(server, {
  beforeShutdown,
}));

Custom response body

app.use(shutdown(server, {
  responseType: 'application/json',
  responseBody: JSON.stringify({ error: 'some error' }),
}));

Custom process kill signals

app.use(shutdown(server, {
  // It will exit on any of these signals
  signals: 'SIGINT SIGTERM',
}));

About

Ensure that during shutdown Koa returns correctly with a 503

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%