How to send response in koa
See original GitHub issueI 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:
- Created 8 years ago
- Comments:38 (16 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
But instead of @madbence’s example I would suggest to do
So once async functions ship in V8 you only have to replace
co.wrap
withasync
andyield
withawait
.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
And, in some external process
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.