【问题标题】:Piping stdout to stdin of another process in node.js在 node.js 中将标准输出管道传输到另一个进程的标准输入
【发布时间】:2013-06-25 16:51:04
【问题描述】:

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

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

【问题讨论】:

  • 你看文档了吗?
  • @Nirk 我读过nodejs.org/api/…,但不确定这是否解决了我的问题。有什么想法吗?
  • 也希望有一些有用的方法来调试这个东西..
  • 阅读nodejs.org/api/… -- 它给出了ps ax | grep ssh的完整示例
  • 我有点困惑,哪个先运行,哪个需要输出作为响应。

标签: node.js pipe stdin


【解决方案1】:

我认为您正在寻找的解决方案是将标准输入从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');
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 2018-12-03
    • 2017-07-24
    • 1970-01-01
    • 2022-07-05
    • 2019-03-17
    • 2013-07-16
    相关资源
    最近更新 更多