Home  >  Article  >  Web Front-end  >  Understanding the process object in Node.js

Understanding the process object in Node.js

青灯夜游
青灯夜游forward
2020-12-01 17:36:243702browse

Understanding the process object in Node.js

Related recommendations: "node js tutorial"

The process object is a global variable, an EventEmitter instance, which provides the current Node.js process information and operation methods

System information

The process object provides attributes for returning key system information. Commonly used ones are

  • title: process name, default value node, the program can be modified to make the error log clearer
  • pid: pid of the current process
  • ppid: pid of the parent process of the current process
  • platform: the operating system on which the process is running (aix, drawin, freebsd, linux, openbsd, sunos, win32)
  • version: Node.js version
  • env: all environment variables of the current Shell

stdin & stdout

Node.js and standard input and output device interaction objects are also provided through the process object

process.stdin.pipe(process.stdout)

A simple line of code can achieve this Print the console input content as it is on the console

Execution information

process.execPath

The process.execPath property returns the Node binary file that executes the current script. Absolute path

process.argv

process.argv property returns an array, the content is the parameters when executing the script, but the first two of the array are fixed

  • The absolute path of the Node binary file that executes the current script

  • The absolute path of the current execution file

process.js

console.log(process.argv);
node process.js a --b=2

[
  '/usr/local/bin/node',
  '/Users/undefined/node-demo/process.js',
  'a',
  '--b=2'
]

process.execArgv

The process.execArgv property returns an array, the members are the command line parameters between the Node executable file and the script file when executing the script under the command line

process.js

console.log(process.execArgv);
node --inspect process.js

[ '--inspect' ]

Common operation methods

  • process.chdir(): Switch the working directory to the specified directory
  • process.cwd(): Returns the path to the working directory where the current script is running, which is the directory when the node command is executed.
  • process.exit(): Exits the current process
  • process.memoryUsage( ): Returns the memory usage of the Node.js process

Process events

The process object is an instance of the EventEmitter object, which can monitor some core system events. Commonly used ones are

exit

When the Node.js process is about to exit due to one of the following reasons, the exit event will be triggered:

  • Explicitly call the process.exit() method
  • Node.js event loop no longer needs to perform any other work

There is no way to prevent exiting the event loop at this time, And once all listeners for the exit event have finished running, the Node.js process will terminate

process.on('exit', (code) => {
  console.log(`退出码: ${code}`);
});

uncaughtException

The current process threw an uncaught error uncaughtException event is triggered

process.on('uncaughtException', function (err) {
  console.error(err.stack);
});

beforeExit

beforeExit## is triggered when Node.js clears its event loop and there is no more work to be scheduled. # event. Normally the Node.js process will exit when there is no scheduled work, but a listener registered on the beforeExit event can make an asynchronous call allowing the Node.js process to continue

process.on('beforeExit', (code) => {
  console.log('进程 beforeExit 事件的代码: ', code);
});

process.on('exit', (code) => {
  console.log('进程 exit 事件的代码: ', code);
});

console.log('此消息最新显示');

// 打印:
// 此消息最新显示
// 进程 beforeExit 事件的代码: 0
// 进程 exit 事件的代码: 0
message

If you use the IPC channel to fork the Node.js process, and the child process receives the message sent by the parent process using

childprocess.send(), the message event

process.on('message', (m) => {
  console.log('子进程收到消息', m);
});
# will be triggered. ##process.nextTick(callback)

The process.nextTick() method adds callback to the queue at the next point in time for execution

For more programming-related knowledge, please visit:

Programming Video course

! !

The above is the detailed content of Understanding the process object in Node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete