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.
this.body
to a readable stream. Your answer (with therequest
module) works becausereq(url)
returns a readable stream. In general, when you want to work with streams directly in Koa/co, you aren't going to be usingyield
. – Bewitch