The Node http module: Building an SEO-Friendly HTTP Server
The http module in Node.js is a powerful tool for building HTTP servers. With its useful functions and classes, developers can easily create and manage networking applications.
Including the http module in your Node.js project is simple:
1 | const http = require('http'); |
The http module provides several properties, methods, and classes that are essential for building HTTP servers. Let’s explore them in detail.
Properties
http.METHODS
This property lists all the supported HTTP methods:
1 | > require('http').METHODS |
http.STATUS_CODES
This property lists all the HTTP status codes and their descriptions:
1 | > require('http').STATUS_CODES |
http.globalAgent
This property points to the global instance of the Agent object, which is an instance of the http.Agent
class. It is used to manage connection persistence and reuse for HTTP clients and plays a crucial role in Node HTTP networking.
Methods
http.createServer()
This method returns a new instance of the http.Server
class. It accepts a callback function that will be called for every incoming request.
1 | const server = http.createServer((req, res) => { |
http.request()
The http.request()
method makes an HTTP request to a server and creates an instance of the http.ClientRequest
class.
http.get()
Similar to http.request()
, the http.get()
method automatically sets the HTTP method to GET and automatically calls req.end()
.
Classes
The http module provides five classes:
http.Agent
Node creates a global instance of the http.Agent
class to manage connection persistence and reuse for HTTP clients. This class is key to improving performance in Node HTTP networking.
The http.Agent
object ensures that every request made to a server is queued and a single socket is reused. It also maintains a pool of sockets.
http.ClientRequest
An http.ClientRequest
object is created when http.request()
or http.get()
is called. When a response is received, the response
event is called with the response and an http.IncomingMessage
instance as an argument.
To access the data returned in the response, you can either call the response.read()
method or set up an event listener for the data
event in the response
event handler.
http.Server
The http.Server
class is commonly instantiated and returned when creating a new server using http.createServer()
. Once you have a server object, you can use its methods:
close()
: Stops the server from accepting new connections.listen()
: Starts the HTTP server and listens for connections.
http.ServerResponse
An http.ServerResponse
object is created by an http.Server
when the request
event is fired. It is commonly known and used in code as res
. You can call the end()
method on the http.ServerResponse
object to close the response and send it to the client. Other useful methods for interacting with HTTP headers include getHeaderNames()
, getHeaders()
, setHeader()
, getHeader()
, removeHeader()
, hasHeader()
, and headersSent()
.
http.IncomingMessage
An http.IncomingMessage
object is created when listening to the request
event on an http.Server
or the response
event on an http.ClientRequest
. It provides various methods to access the response, such as statusCode()
, headers()
, method()
, httpVersion()
, url()
, and socket()
. The data can be accessed through streams, as http.IncomingMessage
implements the Readable Stream interface.
These are the essential components of the http module in Node.js. By leveraging these properties, methods, and classes, developers can efficiently build and manage HTTP servers in their Node.js applications.
Tags: Node.js, HTTP, Networking