As you know, in a node program, if the current process wants to generate a child process, it can invoke the Spawn method of the Child_process module. The Spawn method signature is as follows:
Child_process.spawn (command[, args][, Options])
The properties of the options object stdio are used to control the output of the child process.
When the Options.stdio is set to inherit, the output of the child process is redirected to the StdOut object of the current process, which means that the output of the child process is displayed directly in the current process's control
System.
When the Options.stdio is set to pipe, the output of the child process is redirected to the StdOut object of the Spawn method's return value. The situation is slightly more complicated. I'll give you one of these.
Examples of scenarios. If we want to write the version number of node to a file in the current process, we can write the following code:
1 varFS = require (' FS ');2 varPath = require (' path ');3 var{Spawn} = require (' child_process '));4 5 varChild = Spawn (' node ', ['--version ')], {6Stdio: 'Pipe‘7 });8 9 varFilePath = Path.resolve (' node-version.txt '));Ten varDestination =Fs.createwritestream (filePath); OneChild.stdout.pipe (destination);
The output of the child process is redirected to Child.stdout, Child.stdout is a readable stream, so the data can be written to the final file using its pipe method.
Node: How to control the output of a child process