Introduction to NodeJs
Node.js
It is not a new programming language, nor is it a JavaScript framework, it is a set of foundations V8引擎
to JavaScript运行环境
support the execution of JavaScript code.
Before Node.js
, JavaScript could only run in the browser. With Node.js
, JavaScript is separated from the browser and used directly on the computer like other programming languages, without being restricted by the browser.
About the V8 engine
V8引擎
It is an open source released by Google JavaScript引擎
and is the core of Google Chrome.
Why do you need a JavaScript engine?
When the JavaScript code we write is directly handed over to the browser or Node
executed, the underlying CPU does not know it and cannot execute it. The CPU only knows its own instruction set, and the instruction set corresponds to assembly code. Writing assembly code is a pain. And the instruction sets of different types of CPUs are different, which means that the assembly code needs to be rewritten for each CPU.
JavaScript引擎
JS code can be compiled into assembly code corresponding to different CPUs (Intel, ARM, MIPS, etc.), so that we don't need to read the instruction set manual of each CPU to write assembly code. Of course, JavaScript引擎
your job is not just to compile code, it is also responsible for executing code, allocating memory, and garbage collection.
Reference document: Detailed explanation of Chrome V8 engine
NodeJs core module
assert 断言
If the expression does not meet expectations, an error is thrown, which is used to do some exception catching.
Buffer 缓冲器
The buffer area dedicated to storing binary data is used for binary data flow related processing, mainly used in network protocols, operating databases, processing pictures, receiving and uploading files, etc.
Buffer is available globally, but it is still recommended to refer to it explicitly via the import
or statement.require
events 事件触发器
It provides a property EventEmitter, the core of EventEmitter is event emission and event listener.
cluster 集群
Node runs in a single process. For 32-bit systems, it can use up to 512MB of memory, and for 64-bit systems, it can use up to 1GB of memory. For a computer with multiple CPU cores, such utilization is very low. The cluster module can create multiple node process clusters and realize communication and cooperation between processes.
fs 文件系统
This module provides many very rich and useful functions to access and interact with the file system.
os 操作系统
This module provides utility methods and properties related to the operating system
path
This module provides utilities for working with file and directory paths
console, error
Debug console and exception handling.
throw error, console.log, console.error. Various debugs are used for debugging.
process 当前进程
Get information about the current node process. is a global variable. Some attributes are equivalent to os attributes.
child_process 子进程
Note the difference with process. child_process is a child process opened by node executing a system command. Equivalent to system, popen, exec, etc. in other languages. Directly execute some shell scripts and the like
stream 流
It is an abstract interface for processing streaming data in node. It is mainly used to transmit large files, video, and audio; it is usually segmented.
For example, when watching a video online, the video content is returned to the user through an HTTP request. There are two obvious problems here
1. The video file needs to be read completely before being returned to the user, so the waiting time will be very long.
2. All the video files are put into the memory at one time, and the memory is too much.
The method used stream
can read the video file into the memory bit by bit, and then return it to the user bit by bit.
Many modules in node are stream implementations, such as the return result of child_process execution, the byte stream for opening a file to read, and so on.