Understanding the process object in Node.js

青灯夜游
Release: 2020-12-01 17:36:24
forward
3615 people have browsed it

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)
Copy after login

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);
Copy after login
node process.js a --b=2 [ '/usr/local/bin/node', '/Users/undefined/node-demo/process.js', 'a', '--b=2' ]
Copy after login

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);
Copy after login
node --inspect process.js [ '--inspect' ]
Copy after login

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, theexitevent will be triggered:

  • Explicitly call theprocess.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 theexitevent have finished running, the Node.js process will terminate

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

uncaughtException

The current process threw an uncaught erroruncaughtExceptionevent is triggered

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

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 thebeforeExitevent 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
Copy after login
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(), themessageevent

process.on('message', (m) => { console.log('子进程收到消息', m); });
Copy after login
# 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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!