Nodejs Chapter 14 (process)

processIt is an API for Nodejs to operate and control the current process, and it is a global API mounted under globalThis

API introduction

1. process.arch

Return the CPU architecture of the operating system is the same as the os.arch we talked about before
'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', and 'x64'

2. process.cwd()

Returns the current working directory. For example, the script executed in F:\project\node> returns this directory. It can also be spliced ​​with path instead of __dirname.

image.png

3. process.argv

Get the parameters after the execution process and return an array. It will be useful when we talk about command line interactive tools later. Various cli scaffolds also use this method to accept configuration parameters. For examplewebpack

image.png

4. process.memoryUsage

Used to get the memory usage of the current process. This method returns an object, which contains various memory usage indicators, such as rss (Resident Set Size, resident set size), heapTotal (total size of the heap area), heapUsed (used heap size) and external (external memory usage )wait

{
    
    
    rss: 30932992, // 常驻集大小 这是进程当前占用的物理内存量,不包括共享内存和页面缓存。它反映了进程实际占用的物理内存大小
    heapTotal: 6438912, //堆区总大小 这是 V8 引擎为 JavaScript 对象分配的内存量。它包括了已用和未用的堆内存
    heapUsed: 5678624,  //已用堆大小
    external: 423221, //外部内存使用量 这部分内存不是由 Node.js 进程直接分配的,而是由其他 C/C++ 对象或系统分配的
    arrayBuffers: 17606 //是用于处理二进制数据的对象类型,它使用了 JavaScript 中的 ArrayBuffer 接口。这个属性显示了当前进程中 ArrayBuffers 的数量
  }
5. process.exit()

Calling  process.exit() will force the process to exit as soon as possible, even if there are still pending asynchronous operations that are not fully completed

Example 5 below will not be printed because it is exited at 2 seconds.

image.png

6. process.kill

Similar to exit, kill is used to kill a process, accepting a parameter process id can be obtained through process.pid

process.kill(process.pid)
7. process.env

It is used to read all environment variables of the operating system, and can also modify and query environment variables.

Note that the modification will not really affect the variables of the operating system, but will only take effect in the current thread, and will be released when the thread ends.

image.png

Environment variable scenario

Differentiate between development and production environments

npm install cross-env

What this library does cross-env is to set and use environment variables across platforms, whether it is on a Windows system or a POSIX system. At the same time, it provides a script for setting environment variables, so that you can set environment variables in unix mode in the script, and then it can also run compatible on Windows

usage

cross-env NODE_ENV=dev

image.png

His principle is that if it is windows, call SET, if it is posix, call export to set the environment variable

set NODE_ENV=production  #windows
export NODE_ENV=production #posix

Guess you like

Origin blog.csdn.net/qq1195566313/article/details/132654000