• notice
  • Congratulations on the launch of the Sought Tech site

Detailed explanation of HTTP requests and responses in Node.js

In C# and OC, the client initiates a request, and the server responds. We can understand this process abstractly.

  • 1. When the client initiates a request to the server, it is equivalent to writing a stream to the server (writable)

  • 2. The server reads the client's stream (readable)

  • 3. The response from the server to the client is equivalent to writing a stream to the client (writable)

  • 4. The client reads the server's response (readable)

The whole process is divided into two parts. One is the processing of the client and the processing of the server. The most important thing is the client request and the server response.

1. The http.ClientRequest object

It implements writable stream and can use all its functions. The http.request(options, callback) method is used in the http module to generate this object.

1.options mainly include the following options:

host : server domain name or IP address, default localhost

hostname: Same as host, but with better support for url.parse() than host.

port: port

localAddress: the local interface for the network connection

socketPath: Unix domain socket (use host:port or socketPath)

method: Request method: GET, POST, etc. Default GET

path: The requested resource path defaults to /. It can also include a query string

headers: request headers

auth: authentication

agent: Agent. If Agent is used, the request defaults to Connection: keep-alive. Possible values are undefined, Agent, false

2. Events and methods provided by ClientRequest


response: Emitted when a response to this request is received from the server. The callback handler receives an IncomingMessage object as the only parameter.

socket: Emitted when a socket is assigned to this request.

connect: Sent when the server responds to a request initiated by the CONNECT method. If the event is not handled by the client, the connection will be closed

upgrade: emitted when the server responds to a request that includes an update request in its header

continue: Issued when the server sends a 100ContinueHTTP response, instructing the client to send the request body


write(), end() are the same as writable streams

abort(): Terminate the request.

setTimeout(timeout,[callback]): Set the request timeout time

setNoDelay([noDelay]): noDelay boolean value, true write immediately, false buffer write

setSocketKeepAlive([enable],[initualDelay]): Enables and disables keep-alive for client requests. initialDelay specifies the delay between the last packet and the first keep-alive request.

Second, the http.ServerResponse object

And the http.ClientRequest object also implements the writable stream. It can also implement the function of writable flow, and also includes the following event properties and methods.

Event or property:

  • close: Emitted when Response.End() closes before flushing the response.

  • headersSent: Boolean. Sent as true. Read-only.

  • sendDate: boolean, when true, the Date header is automatically generated and sent as part of the response

  • statusCode: No explicit write header to specify the response status code.


writeContinue(): Send an HTTP/1.1 100 Continue message to the client, requesting the sent body content

writeHead(statusCode,[reasonPhrase],[headers]): Write a response header to the request. The statusCode parameter is the HTTP response status code, and reasonPhrase is a string representing the reason for StatusCode. headers is the response header object. response.writeHead(200,'Success',{'Content-Length':body.length,'Content-Type':'text/plain'});

setTimeout(msecs,callback): Sets the timeout for client connections.

setHeader(name,value): Set the header value.

getHeader(name): Get the header value.

removeHeader(name): remove the header.

addTrailers(headers): Write http trailing headers to the end of the response.

3. Client response and server request

One or two above are mainly writable streams, and readable streams include client responses and server requests. They are all one object: the IncomingMessage object.

You can output it to see what the IncomingMessage object looks like. Since the IncomingMessage object is not often used for our development, it's good to have a look.

var http=require("http")var options={
   method: "GET"};var req=http.request(options,function(response){


Technical otaku

Sought technology together

Related Topic


Leave a Reply