I know that I can pass a stream into the response body in Koa:
import fs from 'fs';
import { Context } from 'koa';
const routeHandler = async (ctx: Context): Promise<void> => {
const stream = fs.createReadStream('path/to/response.png');
ctx.response.set('Content-type', 'image/png');
ctx.body = stream;
};
But let's say I have a large request body. Can I do something like this?
import { Context } from 'koa';
const routeHandler = async (ctx: Context): Promise<void> => {
const stream = ctx.reqest.body; // ???
return new Promise<void>((resolve, reject) => {
stream
.on('data', (chunk) => {
doSomethingWithChunk(chunk);
})
.on('end', () => {
ctx.body = 'Done!';
resolve();
})
.on('error', reject)
});
};
I can't find any documentation around this in koaja/bodyparser. The purpose of this would be to have a large request body but not fill up the memory on the server.