How can I return an error when piping output from a spawned command?

Viewed 194

I'm piping the stdout from a spawned command (ffmpeg, not that it matters) using express, but if the child proc fails I still get a 200.

Is there any way I can return a 500 if the exit code was non-zero? (I think the headers have aleady been sent by then):

const express = require('express');
const { spawn } = require('child_process');

var app = express();

app.get('/video', function(req, res) {
    var cmd = "ffmpeg";
    var args = ["--wat"];
    var proc = spawn(cmd, args);
    res.contentType('video/mp4');
    proc.stdout.pipe(res);
    proc.stderr.pipe(process.stdout);
    proc.on("exit", code => {
        console.log("child proc exited:", code);
        //res.status(code > 0 ? 500 : 200).end();
    }); 
    res.on("close", () => {
        proc.kill("SIGKILL");
    }); 
});

app.listen(4000);
1 Answers
Related