简体   繁体   中英

What use is spawning a child node process and piping it to the current node process?

I am trying to understand this example about using streams in Node from docs.nodejitsu.com.

 var child = require('child_process');

 var myREPL = child.spawn('node');

 myREPL.stdout.pipe(process.stdout, { end: false });

 process.stdin.resume();

 process.stdin.pipe(myREPL.stdin, { end: false });

 myREPL.stdin.on('end', function() {
   process.stdout.write('REPL stream ended.');
 });

 myREPL.on('exit', function (code) {
   process.exit(code);
 });

Reading the code I can see a new node REPL is created in child.spawn('node') and then both its stdin and stdout are piped to the stdin and stdout of the node process running the program.

What would be an useful application of this code; what can I do with it?

一个好用例可能是在“沙盒”环境中评估一些代码,以便逃避的代码不会污染您当前的运行时环境。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM