In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the role of nodejs modularization", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what is the role of nodejs modularization"!
Nodejs modularization benefits: 1, code decoupling, can improve the reusability of the code; 2, can improve the maintainability of the code; 3, can improve the readability of the code; 4, can achieve on-demand loading; 5, can solve naming conflicts; 6, easy to manage dependencies.
This tutorial operating environment: windows7 system, nodejs version 12.19.0, DELL G3 computer.
What is modularization?
In the field of programming, modularization is a fixed rule to split a large file into independent and interdependent modules. Generally speaking, one js file can use data from another js file.
What are the benefits of modularization?
1. Code decoupling can improve the reusability of code.
2. it can improve the maintainability of the code.
3. It can improve the readability of the code
4. It can be loaded on demand.
5. Naming conflicts can be resolved
6. Facilitate the management of dependence
The generation of modularization also brings the module scope, which is similar to the function scope. The variables and methods defined in the custom module can only be accessed in the current scope. This access restriction effectively solves the problem of variable pollution.
Module specification
Modularization specifications are the rules that need to be followed when modularizing and combining code, such as:
What syntax format is used to reference the module
What syntax format is used in the module to expose members to the outside?
The benefits of modular specification: everyone follows the same modular specification to write code, which reduces the cost of communication, greatly facilitates the mutual call between various modules, and benefits others and self-interest.
Module specification category:
CommonJS specification: what is followed in nodejs is the commonjs specification.
ES6 Modularization Specification: (a common modularization specification for front and rear; it can be used in Node.js, Vue, React! )
CMD and AMD Modularization specifications (less used): CMD--sea.js, AMD-require.js
UMD is called the Universal Module definition Specification (Universal Module Definition), and it allows the same code module to run in a project that uses CommonJs, CMD, or even AMD at run time or compile time. It does not have its own proprietary specifications, it is a collection of CommonJs, CMD, AMD specifications in one.
Difference:
1. For dependent modules, AMD is executed in advance and CMD is deferred.
2. CMD favors relying on proximity, while AMD favors relying on the front.
Nodejs module classification
Built-in module-fs (file system module). Path (path module). Http (network operation module).
Const fs = require ("fs"); / / the basic module provided by node, which can be directly imported and used.
Custom module-.js file created by the user
Const m = require (". / js file name"); / / Note that it is the relative path and the suffix .js can be added or not.
Third-party module-A module developed by a third-party individual or team, usually called a package, that needs to be downloaded and imported before use
Const moment = require ('moment'); / / the import and export of the package module needs to be installed through the npm i moment command before use
Use the require () method to load the module
Note: when other modules are loaded using the require method, the code in the loaded module is executed
Export
There is a module object in each custom module, which stores the information related to the current module, and one of the properties, exports, can expose the module members for external use. When you load a module, you get an empty object that the module.exports property points to.
If you want to use the data in this module, you can add attribute values to the property module.exports for external use.
To simplify the code for sharing members, node provides an exports object. By default, exports and module.exports point to the same object.
Note: when you import a module using the require () method, the result of the import is always based on the object pointed to by module.exports.
Misunderstandings in the use of exports and module.exports exports = {username:'ls',gender:' male'} module.exports = exports;module.exports.age ='20 username:'ls',gender:' male / the result after import is {username:'ls',gender:' male', age:'20'} exports = {username:'ls',gender:' male'} module.exports.age ='20 female module.coach = exports / / the result after import is {username:'ls',gender:' male'} exports = {username:'ls',gender:' male'} exports = module.exports;module.exports.age ='20 players / the result after import is {age:'20'}
Therefore, try to use only one of these methods to avoid unnecessary trouble.
At this point, I believe that you have a deeper understanding of "what is the role of nodejs modularization", you might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.