Node provides rich network programming modules
| Node module | protocol |
| net | TCP |
| dgram | UDP |
| http | HTTP |
| https | HTTPS |
TCP service events are divided into the following two categories
(1) Server Event
For servers created through net.createServer(), it is an EventEmitter instance, and there are several custom events:
listening: Triggered after calling listen() to bind port or Domain Socket, abbreviated as server.listen(port, listener), passed in through the second parameter.
connection: triggered when each client socket is connected to the server. The simple way is to pass the last parameter through net.createServer().
close: Triggered when the server is closed. After calling server.close(), the server will stop accepting new socket connections, but keep the currently existing connections and wait for all connections to be disconnected, this event will be triggered.
error: This event will be triggered when an exception occurs on the server.
(2) Connection Event
The server can maintain connections with multiple clients at the same time, a typical writable and readable Stream object for each connection. Stream objects can be used for communication between the server and the client. They can read data sent from one end from one end through data events, or send data from one end to the other end through write() method.
data: When one side calls write() to send data, the other side will trigger the data event. The data passed by the event is the data sent by write().
end: This event will be triggered when either end of the connection sends FIN data.
connect: This event is used for the client and will be fired when the socket connects successfully to the server.
drain: When either end calls write() to send data, the current end triggers the event.
error: When the exception is sent
close: fired when the socket is completely closed
timeout: When the connection is no longer active after a certain period of time, triggering the event to notify the user that the connection is idle.
TCP has certain optimization strategies for small data packets in the network: Nagle algorithm, which is triggered only when the data reaches a certain amount.
UDP Services
UDP is called the User Packet Protocol, and it is not a connection-oriented service. UDP in Node is just an EventEmitter instance, not a Stream instance, with the following custom events:
(1) message: When the UDP socket monitors the network card port and receives the message, the data carried by the trigger is the message Buffer object and a remote address information.
(2) listening: This event is triggered when the UDP socket starts listening.
(3) close: This event is triggered when the close() method is called, and the message event is no longer triggered. If the message event needs to be triggered again, it needs to be rebinded.
(4) error: Triggered when an exception occurs. If it is not listened, it will be thrown directly, causing the process to exit.
HTTP Service
The http module in Node inherits from the tcp server (net module), which can maintain connection with multiple clients. Since it does not create threads for each connection and maintains a very low memory footprint, it can achieve high concurrency. The difference between HTTP service and TCP service is that after keepalive is enabled, a TCP session can be used for multiple requests and responses. The TCP service is used as a unit for connection, and the HTTP service is used as a unit for request. The http module is to encapsulate the process of connecting to request.
The http module abstracts the read and write of the socket used to connect to ServerRequest and ServerResponse objects, corresponding to request and response operations respectively.
(1) HTTP request
For the read operation of the TCP connection, the http module encapsulates it as a ServerRequest object. For example, the header part req.method, req.url, req.headers, the message system data part is abstracted into a read-only stream object. If the business logic needs to read data in the message system, the data stream needs to be completed before the operation can be performed.
(2) HTTP response
HTTP response encapsulates the write operation of the underlying connection, which can be regarded as a writable stream object.
The header information methods of the response packet: res.setHeader() and res.writeHeader() methods. You can setHeader multiple times for setting, but you must call writeHeader to write to the connection before it takes effect.
Partial methods of the message: res.write() and res.end() methods
(3) HTTP server event
connection: When the client establishes a TCP connection with the server, a connection event is triggered
request: After establishing a TCP connection, the HTTP module abstracts the HTTP request and HTTP response from the data stream. When the request data is sent to the server, the event is triggered after the HTTP request header is parsed; after res.end(), the TCP connection can be used for the next request.
close: Calling the server.close method to stop receiving new connections, triggering this event when all existing connections are disconnected.
checkContinue: When some clients send larger data, they first send a request with Expect: 100-continue in the header to the server, and the service triggers the event;
connect: triggered when the client initiates a CONNECT request
upgrade: When the client requires an upgrade of the connection protocol, it needs to negotiate with the server. The client will bring the Updagrade field in the request header.
clientError: The connected client sends an error, and the error is transmitted to the server and the event is triggered.
(4) HTTP client
The http module provides http.request(options, connect) for constructing HTTP clients.
The HTTP client is similar to the server. In the ClientRequest object, its event is called response. When the ClientRequest parses the response message, the response event will be triggered as soon as the response header is parsed. At the same time, a response object ClientResponse is passed for operation. The subsequent response message is provided in a read-only stream.
(5) HTTP client events
response: The client corresponding to the request event on the server triggers the event when the request is responded after the request is issued.
socket: fired when the connection established in the underlying connection pool is assigned to the current request object;
connect: When the client sends a CONNECT request to the server, if the server responds to 200 status code, the client will trigger the event.
upgrade: When the client enjoys the Upgrade request when the client enjoys the server sends an Upgrade request, if the server responds to the 101 Switching Protocols status, the client will trigger the event.
continue: After the client initiates the Expect: 100-continue header information to the server, it attempts to send larger data. If the server responds to the 100 continue state, the server will trigger the event
WebSocket Services
WebSocket first appeared as an important feature of HTML5, and has the following advantages over HTTP:
(1) The client and server only establish TCP connection once, and fewer connections can be used
(2) WebSocket server can push data to the client, which is much more flexible and efficient than HTTP request response mode
(3) Lighter protocol header to reduce data transmission
There is no library built-in WebSocket in Node, but the community's ws module encapsulates the underlying implementation of WebSocket like the famous socket.io