In node. js, the various modules have a standard notation:
This function (Err, stdout, stderr) { callback (err, stdout, stderr); })
The standard here refers to the callback function, which generally has err as the first parameter, then the specific data.
When you write a server program, you will more or less use the child_process module, and the usage of this module is as shown in the above code.
For example, call a shell command to delete a file, so you can:
function (Err, stdout, stderr) { callback (err, stdout, stderr); })
The returned parameter, in fact, err is an object, while stdout and stderr are strings, stdout is the information that executes the child process using standard output , and stderr is the content of the error output stream in the child process.
So the question is, if we use node. js to write a simple script, let the other node program to invoke, how to imitate the implementation of the same return situation?
When other programs are called, this may be the case:
function (Err, stdout, stderr) { callback (err, stdout, stderr); })
If we use Console.log/error to print information in a subprocess, the result is that nothing will be found in the parent process's callback function.
Odd Strange, console.error is not the wrong output? Well, it's only weird that you write more on the web, and then node. JS isn't.
The next step is to introduce three gadgets that correspond to stdout, stderr, and err respectively.
Process.stdout.write
Process.stderr.write
Process.exit (not 0)
The Write function accepts a string, so we can encapsulate it for ease of use:
function () { var msg = Array.prototype.join.call (arguments, ', '); Process.stderr.write (msg);};
Finally, if the program runs out of error, we may need to terminate the program immediately, in addition to outputting the information in stderr.
In accordance with Linux specifications, it is generally successful to use 0 instead of 0 to indicate failure. Then Process.exit also follow this specification.
- Process.exit (0) indicates successful completion, and ERR will be NULL in the callback function;
- Process.exit (not 0) indicates that execution failed, in the callback function, that err is not Null,err.code is the number we passed to exit.
node. JS Standard/Error output and Process.exit