我正在尝试从docs.nodejitsu.com了解有关在Node中使用流的this示例。
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);
});
阅读代码,我可以看到在
child.spawn('node')
中创建了一个新的节点REPL,然后将其stdin和stdout都通过管道传递给运行该程序的节点进程的stdin和stdout。该代码的有用应用是什么?我该怎么办?
最佳答案
一个好用例可能是在“沙盒”环境中评估一些代码,以便逃避的代码不会污染您当前的运行时环境。