Nodejs Chapter 10 (Global Variables)

How to define global variables in nodejs?

Use global to define global variables in nodejs. The defined variables can also be accessed in the imported files. For example, a.js global.xxx = 'xxx' require('xxx.js')xxx.js can also access the variables. In the browser, the global variables we define are all in window, nodejs is global, different environments need to be judged, so a globalThisglobal variable appeared in ECMAScript 2020 , which will automatically switch to global in the nodejs environment, and it is very convenient for the browser environment to automatically switch windows

About other global APIs

Since there is no DOM and BOM in nodejs, except for these APIs, other ECMAscript APIs can basically be used

For example

setTimeout setInterval Promise Math  console  Date fetch(node v18) 等...

These APIs can be used normally

nodejs built-in global API

__dirname

目录It represents the absolute path of the current module

__filename

It represents 文件the absolute path of the current module, including the filename and file extension

require module

Importing modules and module exporting has been discussed in detail in the previous chapter

process
  1. process.argv: This is an array containing command line arguments. The first element is the execution path of Node.js, the second element is the path of the currently executing JavaScript file, and the following elements are the command line parameters passed to the script.
  2. process.env: This is an object containing the current environment variables. You can process.envaccess and manipulate environment variables by.
  3. process.cwd(): This method returns the path of the current working directory.
  4. process.on(event, listener): Used to register event listeners. You can use process.onto listen to events such as exit, uncaughtExceptionand execute the corresponding callback function when the event occurs.
  5. process.exit([code]): Used to exit the current Node.js process. You can provide an optional exit code as an argument.
  6. process.pid: This property returns the PID (process ID) of the current process.

These are just processsome of the commonly used properties and methods of objects, there are many others that can be used to monitor processes, set signal handling, send IPC messages, etc.

需要注意的是,process对象是一个全局对象,可以在任何模块中直接访问,无需导入或定义。

Buffer
  1. 创建 Buffer 实例:

    • Buffer.alloc(size[, fill[, encoding]]): 创建一个指定大小的新的Buffer实例,初始内容为零。fill参数可用于填充缓冲区,encoding参数指定填充的字符编码。
    • Buffer.from(array): 创建一个包含给定数组的Buffer实例。
    • Buffer.from(string[, encoding]): 创建一个包含给定字符串的Buffer实例。
  2. 读取和写入数据:

    • buffer[index]: 通过索引读取或写入Buffer实例中的特定字节。
    • buffer.length: 获取Buffer实例的字节长度。
    • buffer.toString([encoding[, start[, end]]]): 将Buffer实例转换为字符串。
  3. 转换数据:

    • buffer.toJSON(): 将Buffer实例转换为JSON对象。
    • buffer.slice([start[, end]]): 返回一个新的Buffer实例,其中包含原始Buffer实例的部分内容。
  4. 其他方法:

    • Buffer.isBuffer(obj): 检查一个对象是否是Buffer实例。
    • Buffer.concat(list[, totalLength]): 将一组Buffer实例或字节数组连接起来形成一个新的Buffer实例。

请注意,从Node.js 6.0版本开始,Buffer构造函数的使用已被弃用,推荐使用Buffer.alloc()Buffer.from()等方法来创建Buffer实例。

Buffer类在处理文件、网络通信、加密和解密等操作中非常有用,尤其是在需要处理二进制数据时

Guess you like

Origin juejin.im/post/7266009957576884239