0.1.8 • Published 2 months ago

koa2-timeout v0.1.8

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

koa2-timeout

Timeout middleware for koa.

Installation

$ npm install koa2-timeout

Hello koa2-timeout

use javaScript

const Koa = require('koa');
const { timeout } = require('koa2-timeout');
const app = new Koa();
const timeOutHandler = ctx => {
  ctx.body = { code: 408, message: 'request timeout' };
};
app.use(timeout(2000, timeOutHandler));
app.use(async ctx => {
  await new Promise(res => {
    setTimeout(() => {
      res();
    }, 4000);
  });
  ctx.body = 'your response';
});
app.listen(3000, () => {
  console.log('http server is listening at port 3000');
});

use typeScript

import Koa from 'koa';
import { timeout } from 'koa2-timeout';
const timeOutHandler = (ctx: Koa.Context) => {
  ctx.body = { code: 408, message: 'request timeout' };
};
const app = new Koa();
app.use(timeout(2000, timeOutHandler));
app.use(async (ctx: Koa.Context) => {
  await new Promise<void>(res => {
    setTimeout(() => {
      res();
    }, 4000);
  });
  ctx.body = 'your response';
});
app.listen(3000, () => {
  console.log('http server is listening at port 3000');
});
0.1.8

2 months ago

0.1.7

3 months ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.0

3 years ago

0.0.22

3 years ago

0.0.21

3 years ago

0.0.2

3 years ago