Getting Started with Node.js - Modularization

1 Introduction

The content of this article comes from bilibili dark horse programmers

2 The basic concept of modularization

2.1 Modularity

  • 模块化It means that when solving one 复杂问题, go from top to bottom layer by layer 把系统划分成若干模块的过程. For the whole system, the module is 可组合、可分解、可更换的单位.
  • Modularization in the field of programming is to follow 循固定规则and 大文件disassemble one into 独立并相互依赖another 多个小模块.

The benefits of modularity

  1. improve the code复用性
  2. improve the code可维护性
  3. can be realised按需加载

2.2 Modular specification

模块化规范It is the rules that need to be followed when modularizing and combining code.

  1. What grammatical format to use to引用模块
  2. What kind of grammatical format to use? 向外暴露成员
    The benefits of modularization.
    Everyone writes code following the same modularization specification, which reduces communication costs and greatly facilitates mutual calls between modules, benefiting others and themselves.

3 Modularity in node

3.1 Classification of modularity in node

According to different sources of modules, Node.js divides modules into three categories, namely:

  1. Built-in modules: Officially provided by Node.js, such as fs module, path module, http module
  2. Custom module: Every .js file created by the user is a custom module
  3. External modules: modules developed by third parties, not built-in modules officially provided by Node.js, nor custom modules created by users, need to be downloaded before use

3.2 Loading modules

Using the powerful require() method, you can load the required built-in modules, custom modules, and external modules for use

// 加载内置模块
const http = require('http')
// 加载自定义模块
const custom = require('./custom.js')
// 加载外部模块
const moment = require('moment')

Notice:

  • When using the require() method to load other modules, the code in the loaded module will be executed
  • When using require() to load user-defined modules, the suffix of .js can be omitted

4 Module scope in node

4.1 What is module scope

And similarly, the , etc. members 函数作用域defined in custom modules , , such , are called .变量方法只能在当前模块被访问模块级别的访问限制模块作用域

4.2 Benefits of module scope

Prevent global variable pollution problems

4.3 Sharing members of module scope outwards

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

4.3.1 module.exports object

  • In a custom module, you can use the module.exports object to share the members of the module for external use.
  • When the outside world uses the require() method to import a custom module, what is obtained is the object pointed to by module exports

Note when sharing members:
When using the require() method to import a module, the result of the import is always subject to the object pointed to by module exports

// m1.js
const userName = 'zs'

module.exports.userName = userName
module.exports.sayHello = () => console.log('hello');

module.exports = {
    
    
  userName: 'ls',
  sayHi: () => console.log('hello')
}

// test.js
const m1 = require('./m1')
console.log(m1) // { userName: 'ls', sayHi: [Function: sayHi] }
// m1.js
const userName = 'zs'

module.exports = {
    
    
  userName: 'ls',
  sayHi: () => console.log('hello')
}

module.exports.userName = userName
module.exports.sayHello = () => console.log('hello');

// test.js
const m1 = require('./m1')
console.log(m1) // {
    
    
                //   userName: 'zs',
                //   sayHi: [Function: sayHi],
                //   sayHello: [Function (anonymous)]
                // }

4.3.2 exports object

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

4.3.3 Misuse 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:

exports.userName = 'zs'
module.exports = {
    
    
  userName: 'ls',
  age: 23
}
// { userName: 'ls', age: 23 }
module.exports.userName = 'zs'
exports = {
    
    
  userName: 'ls',
  age: 23
}
// { userName: 'zs' }
exports.userName = 'zs'
module.exports.age = 23
// { userName: 'zs', age: 23 }
exports = {
    
    
  userName: 'zs',
}
module.exports = exports
module.exports.age = 23
// { userName: 'zs', age: 23 }

4.4 Modular specification in node

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

CommonJS states:

  1. Inside each module, module 变量representing the current module
  2. The module variable is an object whose export property (ie module.exports) is the external interface
  3. Loading a module is actually loading the module.exports property of the module. The require() method is used to load modules

5 es6 module

ES6 implements module functions at the level of language standards, aiming to replace AMD and CommonJs as common module solutions for browsers and servers.

ES6 Module specifies:

  1. Each js file is an independent module
  2. Import on demand: import , the command is used to import functions provided by other modules
  3. Export on demand: export , the command is used to specify the external interface of the module

Guess you like

Origin blog.csdn.net/weixin_36757282/article/details/128660960