NodeJS

Node.js | HTTP Module

Node.js, known for its asynchronous and event-driven architecture, offers a multitude of built-in modules that make it easy to build scalable and fast web applications. Among these modules, the HTTP module is one of the most crucial as it allows you to create web servers and handle requests and responses effortlessly.

In this blog, we’ll explore the fundamentals of the Node.js HTTP module, including how to set up a basic web server, handle different HTTP methods, and work with headers and status codes.

Introduction to Node.js HTTP Module

Node.js has a built-in module called HTTP, which allows Node.js to transfer data over the HyperText Transfer Protocol (HTTP) as well as communicate with other APIs using HTTP 1.1, HTTP 2, and HTTPS.

Architecture of Node.js HTTP Module

The HTTP module extends two built-in classes:

  • Net module – Provides network API for creating stream-based TCP servers or clients.
  • Events module – Provides an event-driven architecture using EventEmitter class.

Simple Server

To get started, create a JavaScript file on your machine (e.g. app.js) and import the HTTP module.

const http = require('http')
const server = http.createServer((req, res) => { ... });

The function takes two arguments: request object (req), response object (res). Both arguments are stream objects. After creating a server, set a response in the handler (callback function).

//create a server object:
 http.createServer(function (req, res) {
  res.write('Hello World!'); //write a response to the client
   res.end(); //end the response
 }).listen(8080); //the server object listens on port 8080


server.listen(3000, () => {
 console.log('Server started on localhost:3000!');
})

If you run this file (using node app.js), you should see a message

Server started on localhost:3000! printed in the console.

Open a web browser, head over to localhost:3000 and you should see the response from the server displayed in the browser

Conclusion

The Node.js HTTP module is a powerful and flexible tool that allows developers to handle web requests with ease. From setting up simple servers to handling different HTTP methods and working with headers, the HTTP module gives you complete control over how you build web applications.

Whether you’re building a basic website or a full-fledged API, understanding the HTTP module in Node.js is a fundamental step in becoming a proficient backend developer.

Happy Coding!

Click to rate this post!
[Total: 1 Average: 5]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Share
Published by
Bharat Desai

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

3 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago