1. process

Process is a global variable, so it can be used directly without requiring the require operation.

The first is to use it to obtain information about the process, such as the local environment when the process is working, through the process can be obtained. The second process allows you to perform some operations on the current process, such as listening for built-in events during process execution and creating child processes to perform more operations.

1. Obtain memory related information

// Check memory consumption
console.log(process.memoryUsage());
/** * RSS: permanent memory * heapToal: total memory size * heapUsed: used memory * external: extended memory - C/C++ core modules * arrayBuffers: buffer size */
Copy the code

2. Obtain CPU information

console.log(process.cpuUsage());
/** * user: time segment occupied by the user * system: time segment occupied by the system */
Copy the code

3. At runtime, you can view the running directory, Node environment, CPU architecture, user environment, and system platform through Process.

process.cwd(); // Run the directory
process.version; / / the Node version
process.versions; // Run the environment version
process.arch; / / CPU architecture
process.env.Node_ENV; // The environment needs to be set first
process.env.PATH; // Environment variables
process.env.USERPROFILE; // The administrator directory path varies with the environment. Process.env.home
process.platform; // Platform Win32 MacOS

Copy the code

4. Run time can obtain startup parameters, PID, running time,

process.argv; By default, there are two values: the Node directory and the directory where the script is executed.
process.argv0; // Get the first value, there is only one API
process.pid; // Get the running PID
process.ppid; 
process.uptime; // Script run time
Copy the code

Events listen for what is provided in process. I won’t focus on what exactly happens in Process, but rather familiarize myself with event-driven programming and publish-subscribe patterns in NodeJS.

Process implements the EMIT interface. You can use on to listen for events. There are many internal events, such as exit, that are executed when the program exits. Note that the events bound here can only execute synchronous code, not asynchronous code.

process.on('exit'.(code) = > { / / exits
    console.log(code); / / 0
})

process.on('beforeExit'.(code) = > { // Before exiting
    console.log(code); / / 0
})
Copy the code

Exit manually, this exit does not execute beforeExit, and the code after exit does not execute because exit is already exit.

process.exit();
Copy the code

Standard output, input, error

process.stdout; // is a stream that can be read or written to.

process.stdout.write('123'); / / 123
Copy the code
const fs = require('fs');

fs.createReadStream('text.txt').pipi(process.stdout); // Read file output.
Copy the code
process.stdin; // Get the console input
process.stdin.pipe(process.stdout); // Output after input
Copy the code
// Set the character encoding
process.stdin.setEncoding('utf-8');
// Listen for readable events, that is, content
process.stdin.on('readable'.() = > {
    // Get the input
    let chunk = process.stdin.read();
    if(chunk ! = =null) { process.stdout.write(chunk); }})Copy the code