Most of the functional blocks of nodejs exist in the form of modules.
There is usually a unified entry and then different modules are called to complete the functions we need.
Let's first look at how to turn it into a module for the main file to use.
var http = require("http");
...
(...);
"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 preferences. However, I suggest using the module name directly as the variable name, which can make the code readable more.
We modify the code in this way, and we put the code into the start() function and provide the code to other page references through exposures.
var http = require("http");
function start() {
function onRequest(request, response) {
("Request received.");
(200, {"Content-Type": "text/plain"});
("Hello World");
();
}
(onRequest).listen(8888);
("Server has started.");
}
= start;
This way, we can now create our main file and start our HTTP in it, although the server's code is still in.
Create a file and write the following:
var server = require("./server");
();
Execute node
Doing so allows you to put different parts of the application into different files and connect them together by generating modules.
We need to learn about routing in the next section