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
Constructors
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 .
Application building
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 :
We created several projects under the library management module , In this module we can see book.js Follow the common rules JavaScript Constructor Pattern ( But with user.js The constructor example you see in is slightly different ).
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 :
take user.js Change to and book.js With the same constructor mode, our program is working as expected . Here's a hint , We can use Node.js Other languages in the existing JavaScript Write the content . Choose according to your preference .
Here is the code user.js What it looks like after refactoring :
summary
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 .
We also see Node.js Used in applications JavaScript Knowledge of design patterns , And also through some simple examples to illustrate .