Stream stdout from child process to browser via expressjs

后端 未结 2 670
一个人的身影
一个人的身影 2021-02-03 11:35

We have an app built using nodejs, express and child_process.spawn. One requirement is that we need to spawn a process at runtime and capt

2条回答
  •  迷失自我
    2021-02-03 12:08

    The response object of an Express route is also an instance of writable stream, so that allows you to pipe the child process' stdio streams to the response.

    app.get('/path', function(req, res) {
      var child = spawn('ls', ['-al']);
      child.stdout.pipe(res);
    });
    

提交回复
热议问题