node.js - How do I continuously stream to a child process with node spawn? -
using linux's rev program example (reverses input stdin), following code using node's spawn rev take 1 line stdin , reverse it.
var fs = require ('fs'); const readline = require ('readline'); const rl = readline.createinterface ({ input: process.stdin, output: process.stdout }); var spawn = require('child_process').spawn; var rev = spawn ('rev'); rev.stdout.on ('data', function (data) { console.log ('reversed: ' + data); }); rl.on ('line', (line) => { rev.stdin.write(line); rev.stdin.end(); }) so, running , entering abc produces reversed: cba
if remove line rev.stdin.end(); in order enter multiple lines, nothing gets output. how continuous output each line entered interactively such:
abc reversed: cba def reversed: fed ...
Comments
Post a Comment