简体   繁体   English

产生子节点进程并将其管道传输到当前节点进程有什么用?

[英]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. 我想了解这个有关使用节点流从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. 阅读代码,我可以看到在child.spawn('node')创建了一个新的节点REPL,然后将其stdin和stdout都通过管道传递给运行该程序的节点进程的stdin和stdout。

What would be an useful application of this code; 该代码的有用应用是什么; what can I do with it? 我该怎么办?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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