Node provides a rich network programming module
| Node module |
Agreement |
| Net |
Tcp |
| Dgram |
Udp |
| http |
HTTP |
| Https |
HTTPS |
TCP Service TCP service events fall into the following two classes(1), Server eventsFor a server created by Net.createserver (), it is an Eventemitter instance with the following types of custom events:Listening: Triggered after calling the listen () bind port or domain socket, abbreviated to Server.listen (port, listener), passed in by the second parameter.Connection: Each client socket is connected toServerWhen triggered, the concise notation is passed by Net.createserver () and the last parameter.Close: triggered when the server shuts down, after calling Server.close (), the server will stop accepting the new socket connection, but will keep the current connection and wait for all connections to disconnect, triggering the event.Error: This event is triggered when an exception occurs on the server.(2), connection event
The server can remain connected to multiple clients at the same time, and is a typical writable, readable stream object for each connection. The stream object can be used for communication between the server and the client, either by reading data from one end to the other end through a data event, or by sending data from one end to the other through the write () method.
Data: When the end of a call to write () sends data, the other end triggers the data event, and the data passed by the event is the one sent by the write ()End: This event is triggered when the fin data is sent from either end of the connection.Connect: This event is used toClientTriggered when the socket is connected to the server successfully.Drain: This event is triggered by the current end when the write () is called at either end of the send data.Error: When an exception is sentClose: triggered when the socket is completely closedTimeout: When a connection is no longer active after a certain amount of time, the event is triggered to notify the user that the connection is idle. TCP has a certain optimization strategy for small packets in the network: The Nagle algorithm is triggered when the data reaches a certain amount. UDP service UDP is called a user packet protocol, which is not a connection-oriented service. In node, UDP is just a eventemitter instance, not an instance of stream, with the following custom events: (1)message: When the UDP socket listens on the NIC port, it is triggered when the message is accepted, and the data that is carried is the message buffer object and a remote address information. (2)Listening: This event is triggered when a UDP socket starts listening. (3)Close: This event is triggered when the close () method is called, and the message event is no longer triggered. The message event needs to be re-bound if it is to be triggered again. (4)Error: Triggers When an exception occurs, and causes the process to exit if the listener is not directly thrown. HTTP Service node HTTP module inherits from the TCP server (NET module), it can be connected with multiple clients, because it does not create a thread for each connection, maintain a low memory footprint, so high concurrency can be achieved. The difference between the HTTP service and the TCP service is that after the keepalive is turned on, a TCP session can be used for multiple requests and responses. The TCP service is serviced in connection and the HTTP service is serviced in the request unit. The HTTP module encapsulates the process of connection to request. The HTTP module abstracts the read and write of the socket used for the connection to the Serverrequest and Serverresponse objects, respectively, corresponding to the request and response operations. (1)HTTP requestFor a read operation of a TCP connection, the HTTP module encapsulates it as a Serverrequest object. such as the header section Req.method, Req.url, Req.headers, the newspaper stylistic data part of the abstract as a read-only stream object, if the business logic needs to read the data in the newspaper style, it is necessary to the end of this data stream to operate. (2)HTTP ResponseThe HTTP response encapsulates the write operation of the underlying connection, which can be thought of as a writable stream object. The response message header information method: Res.setheader () and Res.writeheader () methods can be set multiple times setheader, but must be called Writeheader write connection to take effect. Part method of newspaper style: Res.write () and Res.end () method(3) HTTP server-side eventsConnection: Triggers a connection event when the client establishes a TCP connection with the serverRequest: After the TCP connection is established, the HTTP Module abstracts the HTTP request and HTTP response from the data stream, and when the request data is sent to the server, the event is triggered after the HTTP request header is resolved; after Res.end (), the TCP connection is available for the next request.Close: The event is triggered when the Server.close method is called to stop receiving a new connection and the existing connection is disconnected.checkcontinue: When sending large data, some clients send a request to the server with Expect:100-continue first, and the service triggers the event;Connect: Triggered when a client initiates a connect requestUpgrade: When a client asks to upgrade a connected protocol, it needs to negotiate with the server, and the client takes the Updagrade field in the request headerClienterror: The connected client sends an error, and the error is passed to the server to trigger the event(4) HTTP clientThe HTTP module provides http.request (options, connect) for constructing an HTTP client. HTTP client and server similar, in the Clientrequest object, its event is called Response,clientrequest when parsing the response message, a parsing of the response header will trigger the response event, It also passes a response object Clientresponse for operation, and subsequent response messages are provided as read-only streams.(5) HTTP client EventsResponse: The event is triggered when the client that corresponds to the request event on the server receives a response after the call is made.Socket: triggered when a 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, the client triggers the event if the service side responds with a 200 status code.Upgrade: When the client-side service sends a upgrade request, the client will trigger the event if the server responds to the 101 switching protocols state.Continue: After the client initiates Expect:100-continue header information to the server to attempt to send large data, if the server responds to the continue state, the server will trigger the event WebSocket service WebSocket was first seen as an important HTML5 feature , compared to HTTP has the following advantages: (1) The client and the server only establish a TCP connection, you can use fewer connections (2) WebSocket server can push data to the client, which is far more flexible and more efficient than the HTTP request response Mode (3) More lightweight protocol header, Reduced data transfer a library with no built-in websocket in node, but the community's WS module encapsulates WebSocket's underlying implementation, such as the famous Socket.io