节点对象转节点
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/
节点对象转节点