With Koa, one of my route has a long process, and I would want to stream the body to start sending the css and the javascript so the browser can start processing it before it gets the data ( Google recommendation )
So this is how I'm trying to do it from a sandbox:
const app = new koa();
const readable = require("stream").Readable;
const s = new readable({ read(size) {} });
app.use(async (ctx, next) => {
ctx.response.set("content-type", "txt/html");
s.push("Hello, World!");
ctx.body = s;
});
app.listen(8080);
When I access the site, instead of seeing the text Hello, World! my browser downloads a file, which contains Hello, World!.
I wonder if what I'm trying to achieve is possible with a stream ?
The trick is that
ctx.typeneeds to be set:This example shows
localhost:8080/)localhost:8080/file)localhost:8080/stream)