koa-timeout

0.1.1 • Public • Published

koa-timeout

Timeout middleware for koa.

Will throw "Request timeout" (Http 408) for any requests that take too long.

Installation

$ npm install koa-timeout

Example

If we always want to respond within half a second, we could use timeout(500) as middleware after our error handler:

var koa = require('koa');
var timeout = require('koa-timeout')(500);
 
var app = koa();
app.use(function * tryCatch(next) {
  try {
    yield next;
  } catch(e) {
    this.status = e.status || 500;
    this.body = e.message;
  }
});
 
app.use(timeout);
 
// Some potentially slow logic:
app.use(function * () {
  yield function(done) {
    setTimeout(done, 1000);
  };
});
 
app.listen(3000);

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i koa-timeout

Weekly Downloads

12

Version

0.1.1

License

MIT

Last publish

Collaborators

  • wejendorp