Please indicate the source for reprinting: Grape City official website, Grape City provides developers with professional development tools, solutions and services, and empowers developers.
Original reference: https://dzone.com/articles/node-dependency-management-part2
In the previous article about Node.js dependency management, we introduced the basics of Node.js modules, how to use module.exports and require methods to handle dependencies, and how folder dependencies work And working principle.
Today we will continue from this point to understand how to use this system module to decompose an application into multiple modules, as well as the advantages of doing so and the working principle of Node.js.
The content of this article mentioned some of the experience sharing in the previous article. Before reading this article, you can read the previous article to learn more.
Before we officially start to learn how to set up an application with multiple modules, let's learn about some other interesting aspects of Node.js modules.
Is Node Modules Singleton Mode
In the previous article, we discussed that Node.js will only load one module at a time. If a request is made, Node.js will give a cached copy of the module. So it seems that these modules behave like only children. Here we look at an example to illustrate this situation.
First, we created a project for the application, initialized the application, and created a file user.js, as shown below:
Next, in APP.JS we will use the user module (user module), and use it as follows:
You can see that we have created two users, as long as one of the variables is modified, the content of the other variable will be affected. We must be aware that the user module will be cached and reused in the time it takes to make another request.
Next, we change it to a constructor and see how it works
Constructor
We introduced the constructor in the previous article and implemented the changes to user.js:
This part of the content will continue to be used in app.js:
We can see two different examples, and can intuitively feel the difference between them. Don't worry too much about different problems, other examples and solutions will be introduced later.
Now we begin to introduce about our topic this time, about how to use modules to manage applications.
Application construction
We start to build a simple application foundation, and then we will continue to refine it during the explanation process.
We have created some new folders, and now the structure included in the program is as follows:
First we created separate folders for different modules. Now there is a library management folder, and the other is for the logger folder, similar to user management. In this way, each of our modules has key points and is easy to locate and manage. At the same time, there are subfolders in each folder. Here we need to pay attention to the index.js file at the folder level, which will serve as the module's API.
Then in app.js, we can reference the module through require and see the project that it works as expected. Notice how we reference it through require user.js.
Now let's update the code content:
We have created several projects under the book management module. In this module, we can see that book.js follows the common JavaScript constructor pattern (but is slightly different from the constructor example seen in user.js).
There is the following code in the corresponding index.js file:
index.js as the module's API, let us use it in app.js as follows:
After changing user.js to the same constructor mode as book.js, our program is working as expected. Here is a reminder that we can use existing JavaScript content in other languages in Node.js to write. You can choose according to your preference.
Here is what the code user.js looks like after refactoring:
to sum up
The content of the application program introduced in this article is very basic. We have conducted file module management by learning some basic knowledge of Node.js dependency management and some common methods of establishing project structure.
We have also seen knowledge about the use of JavaScript design patterns in Node.js applications, and we have also illustrated them with some simple examples.
Further reading
After understanding the dependency management of Node.js, learn more about using the Node package manager to edit the content of
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。