Node js HTTP Module

HTTP is a stateless data transfer protocol built upon a request/response model, client make requests to server, which then return a response.

In Node.js HTTP server, http is the main module. The main methods of http module are as follows -

http.createServer()

A Node server is typically created using the createServer method of the http module. It returns a new web server object.

http.listen()

It starts accepting connections on the specified port and hostname.

http.ServerResponse()

It creates this object internally by an HTTP server, not by the user, and is used as an output of request handlers. It is passed as the second parameter to the 'request' event.

Some of the response types are -

  • response.writeHead(): sends a response header to the request.
  • response.write(): sends a response body.
  • response.setTimeout(): sets the Socket's timeout value to msecs.
  • response.finished(): returns the boolean value that indicates whether the response has completed.

http.STATUS_CODES

It contains the collection of all the standard HTTP response status code.