Let's first implement a simple example, hello world.
It seems that the first section of the tutorial for each language will talk about this, and we are no exception.
First, we create a project directory, which can be defined by ourselves, and the directory of this case is e:/nodetest/.
Since we are building a server, I named the first file server.js.
Enter the following code in server.js:
The code copy is as follows:
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
Then we open cmd.
Use cd e:/nodetest/ to locate it in the project directory, and then execute the node server.js command to run the file;
Then open the browser and visit http://localhost:8888/, and you will see a web page with "Hello World" written on it;
In fact, this is a simple working server, but it’s so simple that it can’t do anything. But it doesn’t matter. Follow me step by step and I will teach you how to build a complete and available server.
In the next section, we will analyze the composition of this code.