Koa: How to catch request cancellation?

Created on 25 Oct 2017  路  1Comment  路  Source: koajs/koa

Hi there! Is there any way to catch request cancellation? Eg. I'm using a stream as response and despite if a request is canceled stream continues to push data to response what causes an error.

Eg.

app.use(async (ctx) => {
  const stream = JSONStream();
  stream.on('error', ctx.onerror);

  ctx.body = stream;

  const qs = new QueryStream('select * from users');

  db
    .stream(qs, s => {
      s.on('error', ctx.onerror);
      s.pipe(stream);
    })
});

Most helpful comment

If anyone is intereseted, you have to use close event on node req

ctx.req.on('close', () => {
  // your logic here...
});

>All comments

If anyone is intereseted, you have to use close event on node req

ctx.req.on('close', () => {
  // your logic here...
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

usernameisalreadytaken2014 picture usernameisalreadytaken2014  路  4Comments

imkimchi picture imkimchi  路  4Comments

edahlseng picture edahlseng  路  3Comments

tracker1 picture tracker1  路  3Comments

tvq picture tvq  路  4Comments