節點對象轉節點
The process
object in Node.js is a global object that can be accessed inside any module without requiring it. There are very few global objects or properties provided in Node.js and process
is one of them. It is an essential component in the Node.js ecosystem as it provides various information sets about the runtime of a program.
Node.js中的process
對象是一個全局對象,可以在任何模塊內對其進行訪問而無需它。 Node.js提供的全局對象或屬性很少, process
就是其中之一。 它是Node.js生態系統中的重要組成部分,因為它提供了有關程序運行時的各種信息集。
To explore we will use one of its properties which is called process.versions
. This property tells us the information about Node.js version we have installed. It has to be used with -p
flag.
為了探索,我們將使用其屬性之一,稱為process.versions
。 此屬性告訴我們有關已安裝的Node.js版本的信息。 它必須與-p
標志一起使用。
$ node -p "process.versions"# output
{ http_parser: '2.8.0',node: '8.11.2',v8: '6.2.414.54',uv: '1.19.1',zlib: '1.2.11',ares: '1.10.1-DEV',modules: '57',nghttp2: '1.29.0',napi: '3',openssl: '1.0.2o',icu: '60.1',unicode: '10.0',cldr: '32.0',tz: '2017c' }
Another property you can check is process.release
that is the same as the command $ node --version
which we used when we installed Node.js. But the output this time is going to be more detailed.
您可以檢查的另一個屬性是process.release
,它與安裝Node.js時使用的命令$ node --version
相同。 但是這次的輸出將更加詳細。
node -p "process.release"# output
{ name: 'node',lts: 'Carbon',sourceUrl: 'https://nodejs.org/download/release/v8.11.2/node-v8.11.2.tar.gz',headersUrl: 'https://nodejs.org/download/release/v8.11.2/node-v8.11.2-headers.tar.gz' }
These are some of the different commands that we can use in a command line to access information that otherwise no module can provide.
這些是我們可以在命令行中使用的一些不同命令,以訪問其他模塊無法提供的信息。
This process
object is an instance of the EventEmitter class. It does it contain its own pre-defined events such as exit
which can be used to know when a program in Node.js has completed its execution.
此process
對象是EventEmitter類的實例。 它確實包含自己的預定義事件,例如exit
,可用于了解Node.js中的程序何時完成執行。
Run the below program and you can observe that the result comes up with status code 0
. In Node.js this status code means that a program has run successfully.
運行以下程序,您會發現結果顯示為狀態碼0
。 在Node.js中,此狀態代碼表示程序已成功運行。
process.on('exit', code => {setTimeout(() => {console.log('Will not get displayed');}, 0);console.log('Exited with status code:', code);
});
console.log('Execution Completed');
Output of the above program:
上面程序的輸出:
Execution Completed
Exited with status code: 0
Process
also provides various properties to interact with. Some of them can be used in a Node application to provide a gateway to communicate between the Node application and any command line interface. This is very useful if you are building a command line application or utility using Node.js
Process
還提供各種屬性進行交互。 其中一些可以在Node應用程序中使用,以提供網關在Node應用程序和任何命令行界面之間進行通信。 如果您要使用Node.js構建命令行應用程序或實用程序,這將非常有用
- process.stdin: a readable stream process.stdin:可讀流
- process.stdout: a writable stream process.stdout:可寫流
- process.stderr: a wriatable stream to recognize errors process.stderr:可識別錯誤的可寫流
Using argv
you can always access arguments that are passed in a command line. argv
is an array which has Node itself as the first element and the absolute path of the file as the second element. From the third element onwards it can have as many arguments as you want.
使用argv
您始終可以訪問在命令行中傳遞的參數。 argv
是一個數組,其節點本身為第一個元素,文件的絕對路徑為第二個元素。 從第三個元素開始,它可以具有任意數量的參數。
Try the below program to get more insight into how you can use these various properties and functions.
嘗試下面的程序,以更深入地了解如何使用這些各種屬性和功能。
process.stdout.write('Hello World!' + '\n');process.argv.forEach(function(val, index, array) {console.log(index + ': ' + val);
});
If you run the above code with the following command you will get the output and the first two elements are of argv
are printed.
如果使用以下命令運行上面的代碼,您將獲得輸出,并打印出argv
的前兩個元素。
$ node test.js# output
Hello World!
0: /usr/local/bin/node
1: /Users/amanhimself/Desktop/articles/nodejs-text-tuts/test.js
翻譯自: https://www.freecodecamp.org/news/node-process-object-explained/
節點對象轉節點