Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 555
chunjw Avatar asked Jun 28 '13 01:06

chunjw


1 Answers

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');
});
like image 124
Lilit Yenokyan Avatar answered Sep 24 '22 02:09

Lilit Yenokyan