Commonly used properties and methods of the process module in Node.js

高洛峰
Release: 2016-12-26 10:04:45
Original
1028 people have browsed it

Foreword

Although node does a lot of abstract work for the operating system, you can still interact with it directly, such as interacting with existing processes in the system and creating working sub-processes. Node is a thread for the event loop, but you can create other processes (threads) outside of this event loop to participate in the work.

If we are not dealing with command line tools, we may have few opportunities to use some methods or attributes in the process module. However, if you want to make a more complex build tool such as webpack or gulp, since the bash interface is a tool for direct communication with users, friendly input and output and complete prompts are very necessary.

Attributes

A table can roughly see what attributes the process has

Commonly used properties and methods of the process module in Node.js

We can use it directly in the code like this

console.log(porcess.platform) // darwin
Copy after login


Using argv will return an array of command lines. We can use the array to obtain the user's specific commands

console.log(process.argv); // [ '/usr/local/bin/node', '/Users/ali-130257n/www/weex-jackzoo/projects/demo.js', '-p', '-v' ]
Copy after login


Under normal circumstances, we want to get the last few parameters. The first two are not needed, we can

let args = process.argv.slice(2); console.log(args) // [ '-p', '-v' ]
Copy after login


Methods

process provides many methods. Generally we can use the following.

cwd: Returns the path to the working directory where the current script is running

abort: Ends the process immediately

nextTick: Specifies the task to run first in the next event loop

process supports some events. Through some events, we can make some friendly prompts or processing.

uncaughtException: When the current process throws an uncaught exception, the uncaughtException event will be triggered

message: accept messages from the parent process

rejectionHandled: use Used to capture the reject

generated by the promise error handling associated with it unhandledRejection: Similarly, this is used to capture the reject

const unhandledRejections = new Map(); process.on('unhandledRejection', (reason, p) => { unhandledRejections.set(p, reason); }); process.on('rejectionHandled', (p) => { unhandledRejections.delete(p); });
Copy after login

that is not associated with the promise error handling


warning: Start when the current process generates a warning

process.on('warning', (warning) => { console.warn(warning.name); // Print the warning name console.warn(warning.message); // Print the warning message console.warn(warning.stack); // Print the stack trace });
Copy after login


Summary

The above is this article I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

For more articles related to commonly used attributes and methods of the process module in Node.js, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
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!