How can I return an error when outputting a pipeline from a generated command?

I pass stdout from the generated command (ffmpeg, this is not important) using an expression, but if the child process does not work, I still get 200.

Is there a way to return 500 if the exit code was nonzero? (I think the headers were 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); 
+5
source share
1 answer

It turns out that the pipe method calls the end of the response. Therefore, the solution must do it 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(); }); 
0
source

Source: https://habr.com/ru/post/1271836/


All Articles