将标准输出管道传输到 node.js 中另一个进程的标准输入

chu*_*njw 5 stdin pipe node.js

我是 node.js 的新手,并试图连续执行两个进程,第一个进程的标准输出通过管道传输到第二个的标准输入。然后第二个进程的标准输出应该通过管道传输到变量 res 作为对 URL 请求的响应。代码在这里。一部分是别人写的,所以我可能有误解:

  var sox = spawn("sox", soxArgs)
  var rubberband = spawn("rubberband", rubberbandArgs)

  sox.stdout.pipe(rubberband.stdin)

  rubberband.stdout.pipe(res) #won't send to res anything, why?
  #rubberband.stdin.pipe(res) won't send to res anything, either!
  #sox.stdout.pipe(res) will work just fine

  sox.stdin.write(data)     
  sox.stdin.end() 
  #the actual sox process will not execute until sox.stdin is filled with data..?
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激!我花了几个小时研究这个!

Lil*_*yan 8

我认为您正在寻找的解决方案是将 stdin 从https://nodejs.org/api/process.html#process_process_stdin通过管道传输到 stdout :

process.stdin.on('readable', () => {
  const chunk = process.stdin.read();
  if (chunk !== null) {
    process.stdout.write(`data: ${chunk}`);
  }
});

process.stdin.on('end', () => {
  process.stdout.write('end');
});
Run Code Online (Sandbox Code Playgroud)