Most of the examples of piping a stream of data using expressjs look like this:
app.get('/video', function(req, res) { var cmd = "ffmpeg"; var args = [...]; var proc = spawn(cmd, args); res.contentType('video/mp4'); proc.stdout.pipe(res); });
Which is great for the happy path, but means any errors from the child proc are returned as a 200; and, in my case, cached. Not ideal.
I googled it pretty hard, and even asked on SO, with no joy. Eventually, I found this article, at which point I realised I’d been asking the wrong question!
The pipe method is part of the base library, nothing to do with express (obvious, in retrospect). And, as the documentation clearly states, it calls end
when the readable stream ends.
So, the solution is to do that bit yourself:
proc.stdout.pipe(res, {end: false}); proc.on("error", err => { console.log("error from ffmpeg", err.stack); res.status(500).end(); }); proc.on("exit", code => { console.log("child proc exited", code); res.status(code === 200 ? 200 : 500).end(); });
Boom! (Just remember to handle all the cases, so end is always called).