chunjw
chunjw

Reputation: 949

Piping stdout to stdin of another process in node.js

I'm new to node.js and trying to execute two processes in a row, the first process' stdout piped into the second's stdin. And then the second process' stdout should be piped into variable res as the response to a URL request. Code is here. Part of it is written by someone else so maybe I have misunderstanding:

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

Any help would be appreciated! I've spent hours looking into this!

Upvotes: 8

Views: 8648

Answers (1)

Lilit Yenokyan
Lilit Yenokyan

Reputation: 369

I think the solution you are looking for is piping stdin to stdout from 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');
});

Upvotes: 8

Related Questions