2.1.0 • Published 7 years ago

koa-connection-limit v2.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

connection limit middlware for koa

Build Status Coverage Status npm Github Releases

Installation

$ npm install koa-connection-limit

API

const koa = require('koa');
const koaConnectionLimit = require('koa-connection-limit');
const app = koa();

app.use(koaConnectionLimit({
  mid: 5,
  high: 10,
  throwError: false,
  pass: (ctx) => {
    return false;
  }
}, function (status) {
  // status: low, mid, high
  console.info(status);
}));

Options

  • mid mid connection limit count
  • high high connection limit count
  • throwError when true or undefined, the connection count reach high limit count, it will throw error
  • pass if the function return true, the request will be ingore of limit
  • err the custom define error, optional

onChange

when status change, the function will be triggered

Example

'use strict';
const Koa = require('koa');
const app = new Koa();
const koaConnectionLimit = require('koa-connection-limit');

// logger

app.use((ctx, next) => {
  const start = new Date;
  return next().then(() => {
    const ms = new Date - start;
    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
  });
});


app.use(koaConnectionLimit({
  high: 2,
  mid: 1,
  throwError: false,
  pass: (ctx) => {
    return ctx.url === '/no-limit';
  },
}, function changeStatus(status) {
  console.info(status);
}));


app.use((ctx, next) => {
  const delay = new Promise(function(resolve, reject) {
    setTimeout(resolve, 3000);
  });
  return delay.then(next);
});

// response

app.use(ctx => {
  ctx.body = 'Hello World';
});

app.listen(3000);

License

MIT

2.1.0

7 years ago

2.0.5

8 years ago

2.0.4

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

0.1.0

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago