close
The Wayback Machine - https://web.archive.org/web/20250214112710/https://www.geeksforgeeks.org/node-js-http-module/
Open In App

Node.js HTTP Module

Last Updated : 16 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Node.js HTTP module allows you to create and manage web servers and handle HTTP requests and responses. Unlike other frameworks like Express.js, the HTTP module is built directly into Node.js, meaning you don’t need to install any additional packages. It provides low-level control over HTTP requests and responses.

What is HTTP Module?

The HTTP module in Node.js provides functionality to create and manage HTTP servers and clients. It includes methods to handle incoming requests, send responses, and interact with HTTP headers and status codes. Since it’s a built-in module, you can use it without installing any third-party libraries.

The HTTP module is fundamental to Node.js and can be used as the backbone for web servers, though many developers prefer to use higher-level frameworks like Express.js to simplify the process.

Syntax:

const http = require('http');

Creating Servers:

The HTTP module allows you to create a server using the http.createServer() method, which listens for incoming requests and handles them using a callback function.

Handling Requests:

You can handle HTTP requests and responses by accessing the request and response objects within the callback function of createServer(). The request object contains data from the client, while the response object is used to send data back.

Making Requests:

The HTTP module also provides methods for making HTTP requests from a Node.js application. You can use http.request() or http.get() to send requests to other servers and handle their responses.

Explore this Node.js HTTP Module Complete Reference to uncover detailed explanations, advanced usage examples, and expert tips for mastering its essential features, enabling you to effectively handle HTTP requests and responses in your Node.js applications

Features

  • Easily create and configure HTTP servers with custom logic.
  • Handle HTTP methods, headers, and data streams.
  • No need for external libraries to handle HTTP in Node.js.

Example 1: In this example, we can create an HTTP server with the help of http.createServer() method. 

JavaScript
// Filename: max.js

const http = require('http');

// Create a server
http.createServer((request, response) => {

    // Sends a chunk of the response body
    response.write('Hello World!');

    // Signals the server that all of
    // the response headers and body
    // have been sent
    response.end();
}).listen(3000); // Server listening on port 3000

console.log("Server started on port 3000");


Step to run this program: Run this max.js file using the below command:

node max.js

Output:

BERJAYA

Node.js HTTP Module

To make requests via the HTTP module http.request() method is used. 

Syntax:

http.request(options[, callback])

Example 2: In this example, we will see to make requests via the HTTP module http.request() method. 

JavaScript
// Filename: max.js 

const http = require('http');

let options = {
    host: 'www.geeksforgeeks.org',
    path: '/courses',
    method: 'GET'
};

// Making a get request to
// 'www.geeksforgeeks.org'
http.request(options, (response) => {

    // Printing the statusCode
    console.log(`STATUS: ${response.statusCode}`);
}).end();


Step to run this program: Run this max.js file using the below command:

node max.js

Output:

http-module

HTTP module http.request()

Benefits

  • Directly manage HTTP transactions with minimal overhead.
  • Build both server and client-side applications with fine-grained control.
  • Seamlessly integrate with other Node.js modules and libraries.

Conclusion

The HTTP module is essential for Node.js developers, providing powerful tools for creating and managing HTTP servers and clients. Its versatility and integration capabilities make it a cornerstone of Node.js application development.


Next Article

Similar Reads

three90RightbarBannerImg