I want to stream an uploaded file with busboy to another server. Doing that I stumbled into strange behavior in error case.
The real application code is in NestJS but I could reduce it to the following in a simple express app. Here is the method from my controller:
app.post("/", (req, res) => {
const bb = busboy({ headers: req.headers });
bb.on("file", (name, file) => {
try {
throw new Error('test');
// Here would be some code that could throw an error.
} catch (error) {
// What do I have to do here? Something like:
// file.destroy();
// bb.destroy();
// req.unpipe(bb);
res.send('error');
}
});
req.pipe(bb);
});
When I upload small files, it works as expected, but when I upload a file of about 10MB it sometimes doesn't send a response. I suspect that the stream isn't properly closed and I commented some code above which I tried but didn't work.
Can someone make sense of it, that it only sometimes doesn't work? And how can I solve it/close the stream properly?