node. JS's Process module

Source: Internet
Author: User

The process module is used to interact with the current process and can be accessed through the global variables process without having to use the Require command to load. It is an instance of a Eventemitter object.

Property

The Process object provides a series of properties for returning system information.

    • Process.pid: The process number of the current process.
    • Process.version:Node versions, such as v0.10.18.
    • Process.platform: Current system platforms, such as Linux.
    • Process.title: The default value is "Node", which you can customize.
    • PROCESS.ARGV: An array of command-line arguments for the current process.
    • PROCESS.ENV: An environment variable that points to the current shell, such as Process.env.HOME.
    • Process.execpath: The absolute path to the executable file that runs the current process.
    • Process.stdout: point to standard output.
    • Process.stdin: point to standard input.
    • Process.stderr: Point to standard error.

The following is an introduction to the main properties.

(1) StdOut

Process.stdout is used to control standard output, which is the display of content to the user in a command-line window. Its write method is equivalent to Console.log.

Exports.log = function () {    process.stdout.write (format.apply (this, arguments) + ' \ n ');};

(2) argv

PROCESS.ARGV returns an array of the various parameters of the command-line script.

Create a new script file Argv.js first.

Argv.js Console.log ("argv:", process.argv); Console.log ("ARGC:", PROCESS.ARGC);

This script is lowered at the command line and the following results are obtained.

Node Argv.js a b C # [' Node ', '/path/to/argv.js ', ' A ', ' B ', ' C ']

The above code indicates that the members of the argv return array are sequentially the parts of the command line. To get the real parameter part, you can rewrite the argv.js to the following.

argv.js var Myargs = Process.argv.slice (2); Console.log (Myargs);
Method

The Process object provides the following methods:

    • Process.exit (): Exits the current process.
    • PROCESS.CWD (): Returns the path to the working directory where the current script is running. _
    • Process.chdir (): Change the working directory.
    • Process.nexttick (): Place a callback function at the top of the next event loop.

Process.chdir () Change the working directory example.

PROCESS.CWD () # '/home/aaa ' Process.chdir ('/home/bbb ') process.cwd () # '/HOME/BBB '

An example of Process.nexttick () that specifies the task to run first in the next event loop.

Process.nexttick (function () {    console.log (' Next event loop! ');});

The above code can be rewritten with settimeout, but the Nexttick effect is higher and the description is more accurate.

SetTimeout (function () {   console.log (' Next event loop! ');}, 0)
Event

(1) Exit event

The exit event is triggered when the current process exits, and you can specify a callback function for the event. This is a good hook (for example, Unit test) to check the status of the module periodically, and the timer defined in the Exit event may not be added to the event list when the main event loop is no longer executed after executing the ' exit ' callback function.

Process.on (' Exit ', function () {  fs.writefilesync ('/tmp/myfile ', ' This must is saved on exit. ');

(2) Uncaughtexception event

The Uncaughtexception event is triggered when the current process throws an unexpected error that is not caught.

Process.on (' Uncaughtexception ', function (err) {   console.error (' An uncaught error occurred! ');   Console.error (Err.stack); });

Content Source: Http://javascript.ruanyifeng.com/nodejs/basic.html#toc22

node. JS's Process module

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.