The common properties and methods of process module in Node.js _node.js

Source: Internet
Author: User
Tags error handling stack trace

Objective

Although node does a lot of abstract work on the operating system, you can still interact with him directly, such as interacting with processes already in the system, creating a working subprocess. node is a thread for an event loop, but you can create other processes (threads) to work outside of this event loop.

If you are not dealing with command-line tools, there may be few opportunities to use some of the methods or attributes in the process module. But if you're going to do a more complex build tool like Webpack or gulp, because the Bash interface is a tool to communicate directly with the user, the full hint is necessary for a friendly input and output.

Property

A table can probably see what properties the process has

Property name Use
Platform Determine the current system platform
Argv Array of command line arguments for the current process
Execpath The absolute path of the executable file for the current process
StdOut Point to standard output
Stdin Point to standard input
StdErr Point to standard error
StdErr Point to standard error

We can use this directly in the code.

Console.log (Porcess.platform) 
//Darwin

Using argv returns an array of command lines that we can use to get user-specific commands

Console.log (PROCESS.ARGV); 
['/usr/local/bin/node ', '/users/ali-130257n/www/weex-jackzoo/projects/demo.js ', '-p ', '-V ']

In general, we would like to get the last of some parameters, the front two do not need, we can

Let args = Process.argv.slice (2); 
Console.log (args)

//[' P ', '-V ']

Method

There are a number of methods available for process. There are some of the following that we can use roughly.

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

abort: End Process immediately

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

Some of the events supported by the process, through some events, we can do some friendly hints or processing.

uncaughtException: When the current process throws an unexpected exception that is not caught, it triggers the Uncaughtexception event

message: Accept messages from the parent process

rejectionHandled: Used to capture the promise error handling associated with it and the resulting reject

unhandledRejection: Similarly this is used to capture reject that are not associated with promise error handling

Const Unhandledrejections = new Map (); 
Process.on (' unhandledrejection ', (Reason, p) => { 
 unhandledrejections.set (p, reason);
});
Process.on (' rejectionhandled ', (p) => { 
 unhandledrejections.delete (p);
});

Warning: The current process produces a warning when it starts

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
});

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.