NodeJS Modular ①

Basic Concepts of Modularity

what is modular

模块化It refers to the process of dividing the system into several modules layer by layer from top to bottom when solving a complex problem. For the entire system, modules are 可组合、分解和更换的单元.

insert image description here

In our lives, this interchangeable game cartridge is the embodiment of modularity.

In the field of programming: modularity, is to follow fixed rules, put alarge filedisassembled into 独立并互相依赖multiple small modules.

The benefits of splitting the code into modules:

  • Improve code reusability
  • Improve code maintainability
  • can be loaded on demand

Modular Specification

Modular specifications are those rules that need to be followed when modularizing and combining code.

E.g:

  • what syntax format to use to refer to modules
  • What syntax format is used in the module to expose members externally

The benefits of the modular specification: Everyone writes code in compliance with the same modular specification, which reduces the cost of communication, greatly facilitates the mutual calls between various modules, and benefits others.

Modularity in Node.js

Classification of modules in Node.js

In Node.js, modules are divided into three categories according to the source of modules, namely:

  • Built-in modules (built-in modules are officially provided by Node.js, such as fs, path, http, etc.)
  • Custom modules (every .js file the user creates, is a custom module)
  • Third-party modules (modules developed by third parties, not officially provided built-in modules, nor user-created custom modules, need to be downloaded before use)

load module

Using the powerful require() method, you can load the required built-in modules, user-defined modules, and third-party modules for use. For example:
insert image description here
Note:

  • When loading the user's custom module, the path to the file is filled in
  • When other modules are loaded using the require() method, the code in the loaded module is executed.
  • When loading a custom module, the file path in it can omit the suffix name

Module scope in Node.js

Similar to function scope, variables, methods and other members defined in a custom module can only be accessed within the current module. This module-level access restriction is called module scope.

E.g:
insert image description here

The benefits of module scope: prevent the problem of global variable pollution
For example:
insert image description here
if two js files are introduced as shown in the figure above, the browser will add both usernames to the global object, and finally print username will get ls. The modular module scope can avoid this very well.

Outwardly share members in module scope

module object
There is a module object in each .js custom module, which stores the information related to the current module, which is printed as follows:

insert image description here
module.exports object
In a custom module, you can use the module.exports object to share the members in the module for external use.
When the outside world uses the require() method to import the custom module, what it gets is the object pointed to by module.exports.
E.g:

// 在一个自定义模块中,默认情况下, module.exports = {}

const age = 20

// 向 module.exports 对象上挂载 username 属性
module.exports.username = 'zs'
// 向 module.exports 对象上挂载 sayHello 方法
module.exports.sayHello = function() {
    
    
  console.log('Hello!')
}
module.exports.age = age

Note when sharing members: When using the require() method to import a module, the result of the import is always based on the object pointed to by module.exports.
As shown in the figure below:
insert image description here

The final printed exports object is a brand new object pointed to later

exports object
Since the word module.exports is more complicated to write, in order to simplify the code of sharing members externally, Node provides the exports object. By default, exports and module.exports point to the same object. The final shared result is still based on the object pointed to by module.exports.

E.g:
insert image description here

Misunderstandings of exports and module.exports :

Always keep in mind that when you require() a module, you will always get the object pointed to by module.exports

Case 1:
insert image description here
Diagram:
insert image description here

Case 2:
insert image description here
Diagram:
insert image description here

Case 3:
insert image description here
Illustration:
insert image description here
Case 4:
insert image description here
Illustration:
insert image description here

To prevent confusion, it is recommended not to use both exports and module.exports in the same module

Modular Specifications in Node.js

Node.js follows the CommonJS modularity specification, which specifies 模块的特性and 各模块之间如何相互依赖.

CommonJS stipulates:
① Inside each module, the module variable represents the current module.
② The module variable is an object whose exports attribute (ie module.exports) is the external interface.
③ Loading a module is actually loading the module.exports attribute of the module. The require() method is used to load modules.

Guess you like

Origin blog.csdn.net/zyb18507175502/article/details/124290989