在Node.js中将stdout导入到另一个进程的stdin

7

我刚接触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..?

非常感谢您的帮助!我已经花费数小时研究了这个问题!


你看过文档了吗? - SheetJS
@Nirk,我看了一下http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options,但不确定它是否解决了我的问题。你有什么想法吗? - chunjw
也希望能提供一些有用的调试方法来解决这个问题。 - chunjw
阅读http://nodejs.org/api/all.html#all_child_process_spawn_command_args_options——它提供了一个完整的“ps ax | grep ssh”的示例。 - SheetJS
看起来 Sox 正常工作,但 rubberband 不行。你确定 rubberband 处理的是从 stdin 输入的内容而不是从某个通过参数传递给它的文件中读取的吗?如果它读取自己的输入而不是给它的输入,那么它就无法正常工作。 - user568109
显示剩余2条评论
1个回答

8

我认为您正在寻找的解决方案是从https://nodejs.org/api/process.html#process_process_stdin将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');
});


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接