Home >Web Front-end >JS Tutorial >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
The process object provides attributes for returning key system information. Commonly used ones are
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
The process.execPath property returns the Node binary file that executes the current script. Absolute path
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' ]
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 lineprocess.js
console.log(process.execArgv);
node --inspect process.js [ '--inspect' ]
The process object is an instance of the EventEmitter object, which can monitor some core system events. Commonly used ones are
When the Node.js process is about to exit due to one of the following reasons, the exit
event will be triggered:
process.exit()
methodThere 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}`); });
The current process threw an uncaught error uncaughtException
event is triggered
process.on('uncaughtException', function (err) { console.error(err.stack); });
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 事件的代码: 0messageIf 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)
For more programming-related knowledge, please visit:
Programming Video courseThe 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!