node.js - 将 stdout 管道传输到 node.js 中另一个进程的 stdin

标签 node.js pipe stdin

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

  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..?

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

最佳答案

我认为您正在寻找的解决方案是将标准输入从 https://nodejs.org/api/process.html#process_process_stdin 传输到标准输出:

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');
});

关于node.js - 将 stdout 管道传输到 node.js 中另一个进程的 stdin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17355815/

相关文章:

bash - POSIX 兼容的 shell 相当于 Bash "while read -d $'\0' ..."?

Python:与 *nix 中正在运行的进程的 STDIN/OUT 交互

arrays - 如何使用node.js在mongodb中插入文档数组?

javascript - 是什么阻止了node.js退出进程?

javascript - 需要检查对象 javascript 中是否存在两个键中的任何一个

c++ - 使用 pthreads 查看 stdin

C - 使用 getchar 而不是 fgets 作为标准输入

node.js - momentjs 返回的日期错误

bash - 如何在 bash 脚本中 fork /管道标准输入?

c - C 中管道的两种方式进程通信