Node.js Modularity
Modular Rain Module
The process of splitting a complex program file into multiple files according to certain rules (standards) is called modularization.
Each file split is a module, and the internal data of the module is private, but the module can expose the internal data for other modules to use
modular project
When coding, the code is coded one by one according to the modules, and the whole project is a modular project.
Modular Benefits
- prevent naming conflicts
- high reusability
- high maintenance
Modular code example
Create two new js files, namely index.js; me.js
writes the test function in me.js, and then exposes the data
function test(){
console.log('testhahhh ');
}
// 暴露数据
// module.exports = test;
Import the module in index.js (modular import me.js script file)
and call the test function
// 导入模块
const me = require('./me.js')
// 调用函数
test();
Requirement upgrade: If there are multiple functions in me.js, call one of them
//新增内容
function spa() {
console.log('spa……');
}
The exposed data is used in the following ways
module.exports = {
spa: spa,
test:test
}
It can be called like this in index.js
// 调用一个
// console.log(me.spa());
me.spa();
How modules expose data
-
module.exports=value
-
exports.name=value
module.exports = 'ilove';
Note :
- module.exports can expose any data
- Data cannot be exposed in the form of exports=value. The implicit relationship between module and exports inside the module exports=module.exports={}
exports = module.exports = {}
console.log(module.exports === exports);
The hidden relationship between the two is as above, but the require import module returns the value of module.exports.
import file module
Use require to pass in the file path in the module to import the file
const test=require('./test.js')
Some precautions in the use of require:
- For the modules created by yourself, it is recommended to write relative paths when importing paths, and ./ and .../ cannot be omitted.
- js and json files can be imported without suffixes, and node extension files written in C/C++ can also be without suffixes, but they are generally not used.
- If the imported path is a folder, the file corresponding to the main attribute in the package.json file under the folder will be monitored first. If the main attribute does not exist, or the package.json does not exist, the index.js under the folder will be monitored And index.json, if it is not found yet, an error will be reported.
- When importing node.js built-in modules, you can directly require the name of the module without adding ./ and .../
The basic process of importing modules
The basic process of require importing custom modules
- Convert a relative path to an absolute path to locate the target file
- cache detection
- read object file code
- Wrap it as a function and execute it (self-executing function). View self-executing functions via argument.callee.toString()
- Cache the value of the module
- Return the value of module.exports
line (self-executing function). View self-executing functions via argument.callee.toString()
- Cache the value of the module
- Return the value of module.exports