koa2-timeout
TypeScript icon, indicating that this package has built-in type declarations

0.1.8 • Public • Published

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');
});

Readme

Keywords

Package Sidebar

Install

npm i koa2-timeout

Weekly Downloads

3

Version

0.1.8

License

MIT

Unpacked Size

4.92 kB

Total Files

6

Last publish

Collaborators

  • jkhuangfu