question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to send response in koa

See original GitHub issue

I understand that when I set this.body and all middleware finished executing the response is sent.

What I want is to send the response at some point and then execute some remaining actions.

Simply setting this.body and then doing yield next doesn’t seem to do it - it doesn’t seem to automatically send the response after the current middleware. I might be wrong, please correct me if this is the case.

This is the simplified code of what I’ve tried:

var router = require('koa-router');
router.get('/test', store, process);

function *store(next) {
  yield storeToDB(this.request.body);
  this.body = 'OK';
  // it seems like I need to do something here to send response, like res.send('OK') in express
  try { yield next; }
  catch(e) { console.log('processing error:', e); }
}

function *process() {
  yield processData(this.request.body);
}

The response is sent after process, not after store as I need.

I understand that some separate process can read from DB on some notifications or schedule, but I wanted to keep it simple yet…

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:38 (16 by maintainers)

github_iconTop GitHub Comments

4reactions
felixfbeckercommented, Nov 4, 2015

But instead of @madbence’s example I would suggest to do

const someSeriousBackgroundOperation = co.wrap(function* () {
  yield verySlowDBQuery();
  yield someOutOfProcessImageProcessingWithCUDA();
  ...
})

app.use(function* (next) {
  yield* next();
  someSeriousBackgroundOperation();
});

So once async functions ship in V8 you only have to replace co.wrap with async and yield with await.

3reactions
yanickrochoncommented, Nov 3, 2015

If speed is an issue, I’d setup my koa app in one process and any other long data manipulation in another process. In pseudo code, this could be something like

var app = createKoaApp();
var pub = createExternalProcessPublisher();

app.use(function *(next) {
  this.body = yield getRequestedInformation(this.req);
  pub.send(collectedRequestInformation(this.req));
});

And, in some external process

var rcv = createReceiver();

rcv.on('message', function (data) {
  /* handle data to log or whatever else with it */
});

This way, the HTTP server is dedicated to return whatever the user wants and nothing else. All other processing and error handling is done externally, independent on the user request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Koa.js - Response Object - Tutorialspoint
Get or set the response body. Usually, we access it using the context object. This is just another way to access it. The...
Read more >
How to send a http response using koajs - node.js
To send the body of a response, you can simply do ctx.response.body = 'Hello' . There are many aliases attached to ctx ,...
Read more >
koa/response.md at master · koajs/koa - GitHub
Check if a response header has already been sent. Useful for seeing if the client may be notified on error. response.lastModified. Return the...
Read more >
How to send JSON response using Koa
In Koa.js you don't need to use some specific methods to make a JSON response. For example, if you send a string:
Read more >
koa.Response JavaScript and Node.js code examples - Tabnine
Most used koa functions · next · Application.use. Use the given middleware `fn`. · Application.listen. Shorthand for: · Request.body · Request.url. Get/Set request ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found