[Simple introduction to node. js] excerpt 2: simple introduction to node. js
Chapter 2 module Mechanism
1. The purpose of the CommonJs specification is to allow JavaScript to run anywhere.
2. For a long time, JavaScript has the following defects:
(1) No Module System
(2) fewer standard databases
(3) No standard interface
(4) Lack of a package management system
The CommonJS specification is proposed to make up for the shortcomings that currently JavaScript does not have standards, so as to provide basic capabilities for developing large applications to Python, Ruby, and Java, rather than staying at the stage of small script programs. They expect that applications written using CommonJS APIs can be executed across the host environment, enabling Javascript to develop Javascript applications on the server, command line applications, desktop GUI applications, and hybrid applications.
3.TODO (CommonJS Specification, Refer to relevant documents)
CommonJs specifications cover module, binary, Buffer, character set encoding, I/O Stream, process environment, file system, socket, unit test, Web Server Gateway Interface, package management, etc.
4. CommonJs module specification.
CommonJS defines a module in a simple way: it contains three parts: module reference, module definition, and module identification.
Var math = require ('Math ');
You can mount the method on the exports object as a property to define the export method.
Exports. add = function (){
}
Each module has its own independent space and they do not interfere with each other.
5. The reference module in Node must go through three steps: Path Analysis, file locating, and compilation and execution.
In Node, modules are classified into two categories: one is the module provided by Node, which is called the core module, and the other is the module written by the user, which is called the file module. During Node source code compilation, the core module compiles binary executable files. When the Node process is started, some core modules are directly loaded into the memory. Therefore, when these core modules are introduced, the two steps of file locating and compilation can be omitted, in addition, it is prioritized in path analysis, so its loading speed is the fastest. The file module is dynamically loaded at runtime. It requires complete path analysis, file locating, compilation and execution, and is slower than the core module.
6. During the module loading process, the Node will first load from the cache, And the cache check of the core module takes precedence over the cache check of the file module. The optimization policy of loading from the cache makes it unnecessary for Path Analysis, file location, and compilation and execution during the secondary introduction, which greatly improves the efficiency of the re-loading module.
7. Several Methods of require Node module (Path Analysis)
(1) core modules such as fs, http, and path
(2) Relative Path starting...
(3) absolute path
(4) non-path modules
8. You can use strace to track the path analysis and loading process of the Node file module:
Strace-o trace-p node module. js
The path analysis process of Node on the file module starts from the current path and goes up step by step until the module or root directory is found.
9. During the require Node module, the identifier does not contain the file extension. Node will be added in the order of. js,. node, and. json. During the attempt, the fs module needs to be called to synchronously block and determine whether the file exists. Because the Node is single-threaded, there may be certain performance problems. One trick is to add the module extension when you use the require file module.
10. During the compilation process, Node wraps the obtained javascript file content, and the generated content is similar:
(Function (exports, require, module ,__ filename, _ dirname ){
Varmath = require ('Math ');
Exports. area = function (radius ){
ReturnMath. PI * radius;
};
});
In this way, each module is isolated by scope, so as not to pollute global variables.
11. Node compilation of. json files is simplest: After Node synchronously reads the content of the JSON file through the fs module, it calls the JSON. parse method to obtain the object. JSON files are useful when used as project configuration files. If you define a json file as the configuration, you do not have to call fs to asynchronously read and parse the file and directly call require for introduction.
12. Node's implementation of module specifications solves code organization issues such as variable dependency and dependency to a certain extent. The emergence of the package further organizes Javascript code on the basis of the module.
13. Node package management tool NPM