In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today I'll show you what the modular approach in Node.js is. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.
1. The basic concept of modularization 1. What is modularization?
Modularization 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 whole system, the module is a unit that can be combined, decomposed and replaced.
Modularization in the field of programming
Is to abide by the fixed rules, a large file into independent and interdependent multiple small modules. The benefits of modularizing the code:
Improve the reusability of the code
Improve the maintainability of the code
It can be loaded on demand.
2. Modular specification
The modularization specification is the rules that need to be followed when the code is divided and combined modularly.
For example:
What syntax format is used to reference the module
What syntax format is used in the module to expose members to the outside?
Benefits of modular specifications:
Everyone follows the same modular specification to write code, which reduces the cost of communication and greatly facilitates the mutual call between various modules, which is beneficial to others and themselves.
2. Modularization in Node.js 1. Classification of modularization in Node.js
According to the different sources of modules, modules are divided into three categories in Node.js, which are:
Built-in modules (built-in modules are officially provided by Node.js, such as fs, path, http, etc.)
Custom module (every .js file created by the user is a custom module)
Third-party module (a module developed by a third party is not an official built-in module, nor is it a user-created custom module, which needs to be downloaded before use)
2. Load module
Using the require () method, you can load the required built-in modules, user-defined modules, and third-party modules for use.
/ / 1. Load the built-in fs module const fs = require ('fs') / / 2, load the custom module const tre = require ('. / tre.js') / / 3, load the third-party module const moment=require ('moment') 3, and share the members of the module scope to the outside
Module.exports object:
There is a module object in each .js custom module that stores information about the current module.
In the custom module, you can use the module.exports object to share the members of the module for external use.
When the outside world imports a custom module with the require () method, you get the object that module.exports points to.
4. Modularization specification in Node.js (1) what is CommonJS module specification
CommonJS is a set of code specifications designed to build an ecosystem of JavaScript outside the browser (server-side, desktop-side).
Through this specification, JavaScript has the ability to develop complex applications and cross platforms.
Node.js follows the CommonJS modularization specification, and CommonJS defines the characteristics of modules and how they depend on each other.
(2) CommonJS modularization specification CommonJS module normalized content export module: moudle.exports export module import module: require ('module name')
CommonJS stipulates that:
1. Inside each module, the module variable represents the current module.
The 2.module variable is an object whose exports property (that is, module.exports) is the external interface.
3. To load a module is to load the module.exports property of the module. The require () method is used to load the module.
(3) each exported module has a moudle object, which contains attributes such as: moudle.exports: indicates the output interface of the current module. Other modules refer to the variable exports exported by moudle.exports: point to moudle.exports. For convenience of operation, it cannot directly point to a value.
(4) use require import module: import and execute a JavaScript file, return an exports object, and report an error if the corresponding object is not found
If the module output is a function, the output interface of the function cannot be exported with the exports variable and must be exported by the user moudle.exports
Load rules:
A. used to load js files with a default file extension of .js
B. Find the corresponding js file under different paths according to the different formats of the parameters.
'. / (.. /)': indicates that the loading path is relative
'/': indicates that the load path is an absolute path
Neither'. / (.. /) 'nor' /': indicates that the loaded mode is the core module of node, in the node_modules of the node installation path
The internal processing flow of require
Require-- > module.exports-- > moudle._load
Third, npm and package 1, the concept of package
What is a bag?
Third-party modules in Node.js are also called packages. Just as computers and computers refer to the same thing, third-party modules and packages refer to the same concept, but with different names.
The source of the bag?
Unlike the built-in modules and custom modules in Node.js, packages are developed by third-party individuals or teams and are available to everyone for free. Note: the packages in Node.js are free and open source and can be downloaded and used free of charge.
Why do you need a bag?
Because the built-in module of Node.js only provides some underlying API, it is very inefficient when developing a project based on the built-in module. The package is packaged based on the built-in module, which provides a more advanced and convenient API, which greatly improves the development efficiency. The relationship between packages and built-in modules is similar to that between jQuery and the browser's built-in API.
2. Installation and uninstallation of packages
Installation of the npm package
Full name of the npm install package
Uninstall npm package--
Full name of the npm uninstall package
Install the designated capital preservation package-
By default, the latest version of the package is automatically installed when you use the npm install command to install the package. If you need to install a specified version of the package, you can specify the specific version with the @ symbol after the package name, for example
Npm install moment@2.12.13, package management profile
Npm states that a package management configuration file called package.json must be provided in the project root directory. Used to record some configuration information related to the project. For example:
The name, version number, description, etc of the project
Which packages are used in the project?
Which packages will be used only during development
Which packages need to be used in development and deployment
The above is the whole content of what the modular method in Node.js is, and more content related to what the modular method in Node.js is can search the previous article or browse the following article to learn ha! I believe the editor will add more knowledge to you. I hope you can support it!
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.