I want to create a koa
route that acts like a proxy for another url, which delivers a file that is usually a few dozens of Megabytes.
Therefore I would like not to block when making the response. I am using this.body = yield request.get(url);
currently, where request is the [co-request
]1 module.
How do I stream the response back to the client ?
Edit :
I am now doing the following :
var req = require('request');
//...
this.body = req(url).pipe(fs.createWriteStream(this.params.what));
If I paste the url
in my browser, I get a file just fine.
However if I get a Error: Cannot pipe. Not readable.
in my route.
Turns out the solution was simply :
var req = require('request');
//...
this.body = req(url);
This is because this.body
has to be a readable stream, which req(url)
returns. Thanks to @danneu for the explanation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With