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 globalThis
global 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
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.process.env
: This is an object containing the current environment variables. You canprocess.env
access and manipulate environment variables by.process.cwd()
: This method returns the path of the current working directory.process.on(event, listener)
: Used to register event listeners. You can useprocess.on
to listen to events such asexit
,uncaughtException
and execute the corresponding callback function when the event occurs.process.exit([code])
: Used to exit the current Node.js process. You can provide an optional exit code as an argument.process.pid
: This property returns the PID (process ID) of the current process.
These are just process
some 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
-
创建
Buffer
实例:Buffer.alloc(size[, fill[, encoding]])
: 创建一个指定大小的新的Buffer
实例,初始内容为零。fill
参数可用于填充缓冲区,encoding
参数指定填充的字符编码。Buffer.from(array)
: 创建一个包含给定数组的Buffer
实例。Buffer.from(string[, encoding])
: 创建一个包含给定字符串的Buffer
实例。
-
读取和写入数据:
buffer[index]
: 通过索引读取或写入Buffer
实例中的特定字节。buffer.length
: 获取Buffer
实例的字节长度。buffer.toString([encoding[, start[, end]]])
: 将Buffer
实例转换为字符串。
-
转换数据:
buffer.toJSON()
: 将Buffer
实例转换为JSON对象。buffer.slice([start[, end]])
: 返回一个新的Buffer
实例,其中包含原始Buffer
实例的部分内容。
-
其他方法:
Buffer.isBuffer(obj)
: 检查一个对象是否是Buffer
实例。Buffer.concat(list[, totalLength])
: 将一组Buffer
实例或字节数组连接起来形成一个新的Buffer
实例。
请注意,从Node.js 6.0版本开始,Buffer
构造函数的使用已被弃用,推荐使用Buffer.alloc()
、Buffer.from()
等方法来创建Buffer
实例。
Buffer
类在处理文件、网络通信、加密和解密等操作中非常有用,尤其是在需要处理二进制数据时