Most of the function blocks of nodejs exist in the form of modules.
Usually there is a unified entrance index.js, and then different modules are called to complete the functions we need.
Let’s first look at how to turn server.js into a module for the index.js main file to use.
"http" is a module that comes with nodejs. We request it in our code and assign the return value to a local variable. We can use this variable to call the object of the public method provided by the http module. The variable name is not fixed. You can name this variable according to your preference. However, I recommend using the module name directly as the variable name, which can make the code More readable.
Let’s change the code in server.js in this way. We put the code in the start() function and provide the code to other pages for reference through expors.
With this, we can now create our main file index.js and start our HTTP in it, although the server code is still in server.js.
Create the index.js file and write the following content:
Execute node index.js
This way you can put different parts of the application into different files and connect them together by generating modules.
In the next section we will learn about routing