博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
节点对象转节点_节点流程对象说明
阅读量:2520 次
发布时间:2019-05-11

本文共 3944 字,大约阅读时间需要 13 分钟。

节点对象转节点

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 CompletedExited 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# outputHello World!0: /usr/local/bin/node1: /Users/amanhimself/Desktop/articles/nodejs-text-tuts/test.js

翻译自:

节点对象转节点

转载地址:http://slrwd.baihongyu.com/

你可能感兴趣的文章
原生安卓去除网络叉号
查看>>
命令行下设置串口并发送
查看>>
SEO前端搜索引擎优化
查看>>
Entity Framework4.0 (四) EF4的内部结构和几个基本概念(转)
查看>>
WCF大数据量传输的详细步骤
查看>>
Objective-C之词典对象
查看>>
数据结构学习笔记3.2—快速排序
查看>>
ShellApi 列举正在运行的程序
查看>>
转:关于JAVA多线程同步
查看>>
Javascript之UI线程与性能优化
查看>>
实现toggleClass功能
查看>>
设计Web2.0图--Aeromatex
查看>>
nginx动静分离配置
查看>>
jQuery 两种方法实现IE10以下浏览器的placeholder效果
查看>>
poj2253 最短路 floyd Frogger
查看>>
springboot:session集中存储到redis
查看>>
《Python编程快速上手+让繁琐工作自动化》第12章实践项目:空行插入程序
查看>>
POJ 2986 A Triangle and a Circle(三角形和圆形求交)
查看>>
css3最新技术教程
查看>>
【tool】测试驱动开发全攻略
查看>>