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

Magento 2: How to Add View Button in Admin Grid to Open a View Page in Slide Window

Hello Magento Friends, In Magento 2, customizations to the admin panel can significantly enhance the…

12 hours ago

Magento 2: How to Observe the Multi-shipping Order Creation Event

Hello Magento Friends, Magento 2 provides a robust event-driven architecture that allows developers to observe…

3 days ago

Hyvä Theme FAQs

Hyvä is gradually gaining popularity in this current market, and even 3.5% of Magento websites…

4 days ago

What is Curbside Pickup?

In today’s fast-paced society, where convenience and safety are paramount, curbside pickup has emerged as…

4 days ago

What is a Planogram?

Have you ever observed how complementary and similar items are often displayed together in brick-and-mortar…

4 days ago

Hyvä Checkout – A Real Game Changer

You may be familiar with Hyvä, the frontend theme for Magento 2, which has been…

4 days ago