process is a global built-in object that can be accessed from anywhere in your code, which represents the operating system process object that hosts our node. js code.
Process objects can be used to intercept events such as exceptions, exits, and so on, to get information about the process's current directory, environment variables, memory consumption, and to perform operations such as process exit, working directory switching, etc.
1. When we want to view the current directory of the application, we can use the CWD function:
PROCESS.CWD ();
2. If you need to change the application directory, you should use the ChDir function:
Process.chdir (' directory name ');
3.stdout is the standard output stream, which is the function of printing content to the output device, Console.log is encapsulated it:
Process.stdout.write (' content to output ');
The 4.stderr is the standard error stream, which is similar to stdout, but it is used to print error messages that we can use to capture error messages:
Process.stderr.write (' content to output ');
5.stdin is the input stream of the process, and we can get the input by registering the event:
// to get the input by registering an event function // Gets the contents of the input stream var chunk = process.stdin.read () ; if NULL {// output data+ The contents of the input stream process.stdout.write (' data: ' + chunk); }});
6. If you need to kill the process within the program, exit the program, you can use the Exit Function:
// The code for the parameter is returned after exit, and if omitted, 0process.exit (code) is returned by default;
7. Get stdout Content: Add registration Events
Process.stdout.on (' data ',function(data) { console.log (data);});
8. In the content of our input and output when there is a Chinese language, the problem may be garbled, this is due to different coding, so in this case you need to set the code for the stream:
Process.stdin.setEncoding (' UTF8 ');p rocess.stdout.setEncoding (' UTF8 ');p rocess.stderr.setEncoding ( ' UTF8 ');
9. More Process Usage:
http://nodeapi.ucdok.com/#/api/process.htmlhttp://nodejs.cn/api/process
node. JS Learning Notes under Windows (8)---process management processes