Reprint please indicate the source ： Grape city official website , Grape city provides professional development tools for developers 、 Solutions and services , Enabling developers .
Original reference ：https://dzone.com/articles/node-dependency-management-part2
I'll introduce you to Node.js In the article of dependency management , We introduced about Node.js Basic knowledge of modules , How to use module.exports and require Method to handle dependencies , And folder dependencies （folder dependencies） How it works and how it works .
Today we will continue to start from that , To learn how to use this system module to break down an application into multiple modules , And the advantages and Node.js How it works .
In this article, I mentioned some experience sharing in the first part , Before reading this article, you can read the first part to learn more about .
Before we start learning how to set up an application using multiple modules , Let's get to know Node.js Some other interesting aspects of the module .
Node Modules Is it singleton mode
In the last article , We talked about Node.js Only one module is loaded at a time . If a request is made ,Node.js A cached copy of the module is given . So it looks like these modules behave like an only child . Here's an example , To illustrate this situation .
First , We created a project for the application , Initialize the application , And created a file user.js, As shown below ：
next , stay APP.JS We'll use the user module in （user module）, And use it as follows ：
You can see that we created two users , As long as one of the variables is modified, the content of the other variable will be affected . We have to be aware that user modules are cached , And is reused in the time of another request .
Now let's change it to a constructor , See how he works
We introduced constructors in the last article , Realized user.js Change of ：
This part will continue in the following app.js Use in ：
We can see two different examples , And you can intuitively feel the difference between them . Don't worry too much about different problems , Other examples and solutions will be presented later .
Now let's start with our topic , About how to use modules to manage applications .
Let's start building a simple application foundation , Next, we will continue to refine it in the process of explanation .
We've created some new folders , Now the structure included in the program is as follows ：
First, we create separate folders for different modules . Now there are library management folders , The other is for the logger folder , Similar to user management . In this way, each of our modules has a focus , Easy to locate and manage . At the same time, there are subfolders in each folder . Here we need to pay attention to the folder level index.js file , It's going to be a part of the module API.
And then in app.js We can pass require Refer to the module , See the project that it works as expected . Notice how we get through require user.js To quote it .
Now let's update the code content ：
Corresponding index.js The following code is in the file ：
index.js As a module API, Let us in app.js Use it in , As shown below ：
Here is the code user.js What it looks like after refactoring ：
The application content described in this article is very basic , We learn Node.js Some basic knowledge of dependency management and some common methods of establishing project structure are used for file module management .