节点对象转节点_节点流程对象说明
節(jié)點(diǎn)對(duì)象轉(zhuǎn)節(jié)點(diǎn)
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對(duì)象是一個(gè)全局對(duì)象,可以在任何模塊內(nèi)對(duì)其進(jìn)行訪問而無需它。 Node.js提供的全局對(duì)象或?qū)傩院苌?#xff0c; process就是其中之一。 它是Node.js生態(tài)系統(tǒng)中的重要組成部分,因?yàn)樗峁┝擞嘘P(guān)程序運(yùn)行時(shí)的各種信息集。
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 。 此屬性告訴我們有關(guān)已安裝的Node.js版本的信息。 它必須與-p標(biāo)志一起使用。
$ 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.
您可以檢查的另一個(gè)屬性是process.release ,它與安裝Node.js時(shí)使用的命令$ node --version相同。 但是這次的輸出將更加詳細(xì)。
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對(duì)象是EventEmitter類的實(shí)例。 它確實(shí)包含自己的預(yù)定義事件,例如exit ,可用于了解Node.js中的程序何時(shí)完成執(zhí)行。
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.
運(yùn)行以下程序,您會(huì)發(fā)現(xiàn)結(jié)果顯示為狀態(tài)碼0 。 在Node.js中,此狀態(tài)代碼表示程序已成功運(yùn)行。
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: 0Process 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還提供各種屬性進(jìn)行交互。 其中一些可以在Node應(yīng)用程序中使用,以提供網(wǎng)關(guān)在Node應(yīng)用程序和任何命令行界面之間進(jìn)行通信。 如果您要使用Node.js構(gòu)建命令行應(yīng)用程序或?qū)嵱贸绦?#xff0c;這將非常有用
- process.stdin: a readable stream process.stdin:可讀流
- process.stdout: a writable stream process.stdout:可寫流
- process.stderr: a wriatable stream to recognize errors process.stderr:可識(shí)別錯(cuò)誤的可寫流
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您始終可以訪問在命令行中傳遞的參數(shù)。 argv是一個(gè)數(shù)組,其節(jié)點(diǎn)本身為第一個(gè)元素,文件的絕對(duì)路徑為第二個(gè)元素。 從第三個(gè)元素開始,它可以具有任意數(shù)量的參數(shù)。
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.
如果使用以下命令運(yùn)行上面的代碼,您將獲得輸出,并打印出argv的前兩個(gè)元素。
$ 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/
節(jié)點(diǎn)對(duì)象轉(zhuǎn)節(jié)點(diǎn)
總結(jié)
以上是生活随笔為你收集整理的节点对象转节点_节点流程对象说明的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript标准对象:地图
- 下一篇: 解释什么是快速排序算法?_解释排序算法