Reputation: 123
I am new to nodejs stream. I try to connect the child process stream with the parent's, but it doesn't work. Can someone tell me what's wrong with the code? here is my code.
var child = require('child_process');
var ps = child.spawn('wc', '-l');
ps.stdout.pipe(process.stdout, {end: false});
process.stdin.pipe(ps.stdin, {end:false});
ps.stdin.on('end', function(){
process.stdout.write('ps stream ended');
});
ps.on('exit', function(code){
process.exit(code);
});
Upvotes: 2
Views: 159
Reputation: 4336
There are 2 things with this code.
First, the second argument of .spawn
should be an array.
Second, you basically need to pass a filename to wc to get any output.
So for example if you do something like
var ps = child.spawn('wc', ['-l', 'package.json']);
or
var ps = child.spawn('ls', ['-a']);
It works just fine.
Upvotes: 1