Hey reader👋Hope you are doing well😊
In the last post we have discussed about File System module in NodeJS. In this post we are going to discuss about HTTP module in NodeJS.
So let's get started🔥
What is HTTP?
HTTP stands for HyperText Transfer Protocol. It is a protocol for fetching resources such as HTML documents. It is the foundation of any data exchange on the Web and it is a client-server protocol, which means requests are initiated by the recipient, usually the Web browser.
HTTP Module
What is HTTP Module?
Node.js has a built-in module called HTTP, which allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP).
To include the HTTP module-:
const http=require('http')
or
import 'http'
Why is HTTP Module used?
HTTP module is used for server side development. The module provides different functionalities to easily create and manage HTTP servers and clients. The HTTP module offers a non-blocking, event-driven architecture that leads to better scalability and performance.
In essence, the HTTP module makes it easy for developers to build web applications by handling HTTP requests and responses, managing connections, and providing support for various HTTP methods (such as GET, POST, PUT, DELETE). By leveraging the HTTP module, developers can quickly create efficient and high-performing servers without the need for extensive setup or configuration.
Main feature of HTTP module
Creating HTTP server -: The HTTP module allows you to create an HTTP server using the
createServer()
method. This method returns an instance of the Server class, which can listen for incoming requests and respond with the appropriate content.Event-driven architecture -: The HTTP module in Node.js follows an event-driven, non-blocking I/O model. This means that the server and client objects use events to handle communication, allowing for efficient and scalable network applications.
Creating HTTP client -: The HTTP module offers the
request()
andget()
methods for making HTTP requests to external servers. These methods return an instance of the ClientRequest class, which can be used to send and receive data from remote servers.Request and response objects -: The http.Server class emits a 'request' event whenever a new request is received. The event listener receives two arguments: the request object (IncomingMessage) and the response object (ServerResponse). These objects contain methods and properties to read and manipulate the request and response data, such as headers, status codes, and body content.
Streaming data -: The HTTP module supports streaming data for both requests and responses. This allows efficient handling of large files or continuous data streams without consuming excessive memory.
Connection management -: The HTTP module provides methods for managing server and client connections, such as setting timeouts, keep-alive settings, and connection pooling.
HTTPS support -: In addition to HTTP, the module provides support for HTTPS (secure HTTP) via the https module, which is built on top of the HTTP module. This enables the creation of secure servers and clients using SSL/TLS encryption.
So this was basic introduction of HTTP module in NodeJS in the next blog we will see different functionalities of this module. ill then stay connected and don't forget to follow me.
Thankyou🤍
Top comments (0)