as explained in Baidu Encyclopedia:
Hyper Text Transfer Protocol (HTTP) is a simple request-response protocol that usually runs on top of TCP. It specifies what kind of messages the client may send to the server and what kind of response it gets. The headers of request and response messages are given in ASCII form; [9] the message content has a MIME-like format. This simple model was responsible for the early success of the Web because it made development and deployment very straightforward.
If you have learned the basics of JavaSE, you should be familiar with network programming.
Of course, it’s okay if you haven’t. Listen to the conscientious author and let me tell you:
1.1. Network communication protocol.
Today in 2022, computer networks have become people’s daily routine. The necessities of life, whether it is emails, instant messaging with friends, short video entertainment... It can be said that we can connect multiple computers through computer networks.
Computer networks connect multiple computer devices under a network through transmission media, communication facilities, and network communication protocols, realizing resource sharing and data transmission.
However, when computers on the same network connect and communicate, they must abide by certain rules. In computer networks, these rules for connection and communication are called network communication protocols:
The http protocol we are talking about here is implemented based on tcp. A common http application scenario is that you enter a string of addresses in the browser and then return a web page.
1.2. IP address and port number
In order to enable computers in the network to communicate, each computer must be assigned an identification number. This identification number can be used to specify the computer that receives data or the computer that sends data.
Check the IP address of your computer on the LAN. Press WIN+R on the Windows computer and enter cmd to quickly enter the console
ipconfig.
You can connect to the specified computer through the IP address, but if you want to access one of your applications on the target computer, you also need to specify the port number.
For example, MySQL's 3306, TomCat's 8080
Node.js provides the http module. The http module is mainly used to build HTTP servers and clients. To use the HTTP server or client functions, you must call the http module.
2.1, thick accumulation (detailed introduction, detailed introduction of the object methods used, the entire http service construction process)
process introduction:
first use the createServer() method to register the server object,
and then use this server object to call the on() method to monitor and process events.
Call the listen() method to bind the port number
to get started:
any network service application always creates a service object first. In nodeJS we can use the createServer method to achieve this,
// First import the http module const http = require('http'); //Create http service object const server = http.createServer();
The Server object returned by the createServer constructor is an event emitter. Here, the created server object is used to process it using its own on() method. Event listening and processing. In this way, whenever an http request is sent, we can process it.
// First import the http module const http = require('http'); //Create http service object const server = http.createServer(); // Bind event listening server.on('request', (request, response) => { //Always believe that good things are about to happen! });
We introduced (IP address + port) before. When our computer is connected to the Internet, the router will automatically assign the IP address to us through DHCP, but if we want to access the specified program on the computer, we must also have a port number.
In order to access the specified program on the computer, we also need to use the listen() method. You only need to use server.listen() to pass the port number as a parameter into the listen method as the listening port.
// First import the http module const http = require('http'); //Create http service object const server = http.createServer(); // Bind event listening server.on('request', (req, res) => { // The content of this function is just a small call to the res parameter to make the program run more understandably // Write a response header (the browser will not recognize it if you don't write it) res.writeHead(200,{'Content-Type':'text/html;charset=UTF8'}); //Send response data res.end("<h1>Welcome to use node.js to build services</h1>"); }); //Bind port number server.listen(8888); //The console prints the address to facilitate quick debugging console.log('Your http service is started at http://127.0.0.1:8888/');
Code running demonstration:
The above code demonstration is very detailed, but in actual development, it is not recommended to write it step by step. It is too cumbersome.
Next, follow the author and let us continue to optimize the code to make it more powerful and concise.
2.2, thin hair (minimalism is king) , elegant! So elegant! )
The process of registering objects step by step is too cumbersome. Here we use a small and beautiful method to implement an http interface:
const http = require('http' ); const server = http.createServer(function(req,res){ //Always believe that good things are about to happen}).listen(8080);
Whenever an HTTP request reaches the server, the function passed in createServer is automatically executed. So this function is also called the request processing function. We can directly pass in the event listening callback function, and then click the listen() method to directly bind the port number.
But this is not good enough. Yes, it can be better. Modify the above callback function with an arrow function to make it more beautiful.
const http = require('http'); const server = http.createServer((req,res) => { //Always believe that good things are about to happen}).listen(8080);
Of course
this
is not good enough
It can be better!
Directly createServer() solves everything:
var http = require('http') // Create server http.createServer( (req, res) =>{ //Always believe that good things are about to be sent}).listen(8888);
Seeing this, congratulations on getting started with the http module of nodeJS. At this moment, you have mastered the following skills:
Please continue to follow the author. Next we will learn to