node. JS Standard/Error output and Process.exit

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.