DEV Community

Cover image for πŸš€ Day 14 of My Node.js Learning Journey: Understanding the HTTP Module in Node.js
Krati Joshi
Krati Joshi

Posted on

πŸš€ Day 14 of My Node.js Learning Journey: Understanding the HTTP Module in Node.js

As I continue my Node.js learning journey, today's focus was on one of the most fundamental concepts of backend developmentβ€”the HTTP Module.

Before learning frameworks like Express.js, it's important to understand how Node.js handles HTTP requests and responses under the hood. Since Express is built on top of the HTTP module, mastering this concept makes debugging and backend development much easier.


🌐 What is HTTP?

HTTP (HyperText Transfer Protocol) is the protocol that enables communication between a client (browser, mobile app, or Postman) and a server.

Every time you visit a website, the following happens:

Browser
   β”‚
HTTP Request
   β”‚
   β–Ό
Node.js Server
   β”‚
HTTP Response
   β–Ό
Browser
Enter fullscreen mode Exit fullscreen mode

The client sends a request, the server processes it, and then returns a response.


πŸ“¦ What is the HTTP Module?

The HTTP module is a built-in Node.js module that allows us to create web servers and handle HTTP requests without installing any external packages.

Importing the module is simple:

const http = require("http");
Enter fullscreen mode Exit fullscreen mode

Since it's a core module, no installation is required.


πŸ› οΈ Creating Your First HTTP Server

One of the first things I learned was how easy it is to create a basic HTTP server.

const http = require("http");

const server = http.createServer((req, res) => {
    res.end("Hello, Node.js!");
});

server.listen(3000, () => {
    console.log("Server is running on port 3000");
});
Enter fullscreen mode Exit fullscreen mode

After running this program and opening http://localhost:3000, the browser displays the response sent by the server.


πŸ” Understanding req and res

The callback inside createServer() receives two important objects:

req (Request Object)

Contains information sent by the client, such as:

  • Requested URL
  • HTTP Method
  • Headers
  • Client IP

Some commonly used properties are:

  • req.url
  • req.method
  • req.headers

res (Response Object)

Used to send data back to the client.

Some frequently used methods include:

  • res.write()
  • res.end()
  • res.setHeader()
  • res.writeHead()

One important thing I learned today:

Always call res.end() to complete the response. Without it, the client keeps waiting because the server doesn't know the response has finished.


🧭 Simple Routing Without Express

Even without Express.js, we can create basic routes using conditions.

if (req.url === "/") {
    res.end("Home Page");
} else if (req.url === "/about") {
    res.end("About Page");
} else {
    res.statusCode = 404;
    res.end("Page Not Found");
}
Enter fullscreen mode Exit fullscreen mode

This helped me understand how frameworks like Express simplify routing.


πŸ“Œ Common HTTP Methods

Every API uses HTTP methods to perform different operations.

Method Purpose
GET Retrieve data
POST Create new data
PUT Replace existing data
PATCH Update part of existing data
DELETE Remove data

These methods form the foundation of REST APIs.


πŸ“„ HTTP Status Codes

Servers use status codes to indicate the result of a request.

Some commonly used ones are:

βœ… Success

  • 200 β€” OK
  • 201 β€” Created

❌ Client Errors

  • 400 β€” Bad Request
  • 401 β€” Unauthorized
  • 403 β€” Forbidden
  • 404 β€” Not Found

πŸ”₯ Server Errors

  • 500 β€” Internal Server Error

Learning these status codes is essential for backend interviews and API development.


πŸ“¦ Headers and MIME Types

HTTP headers carry metadata about requests and responses.

One commonly used response header is:

res.setHeader("Content-Type", "application/json");
Enter fullscreen mode Exit fullscreen mode

This tells the browser that the response contains JSON data.

Some common MIME types include:

  • application/json
  • text/html
  • text/plain
  • image/png
  • application/pdf

⚑ HTTP Module vs Express.js

One question that often comes up is:

"If Express exists, why learn the HTTP module?"

The answer is simple.

The HTTP module provides the core functionality, while Express builds on top of it by adding features like routing, middleware, and request parsing.

Understanding the HTTP module gives you a much clearer picture of what happens behind the scenes whenever an Express application receives a request.


🎯 Key Takeaways

Today I learned:

  • HTTP enables communication between clients and servers.
  • Node.js provides a built-in HTTP module for creating servers.
  • createServer() creates an HTTP server.
  • req contains request information.
  • res is used to send responses.
  • res.end() is mandatory to complete the response.
  • Routing can be implemented without Express.
  • HTTP methods define CRUD operations.
  • Status codes communicate the outcome of a request.
  • Express.js is built on top of the HTTP module.

πŸ’­ Final Thoughts

Today's session helped me understand what happens behind every API request.

Whenever we use Express.js, call an API, or open a webpage, the HTTP module is working behind the scenes. Learning it first makes backend concepts much easier to understand and gives a stronger foundation for building scalable server-side applications.

Every day of learning brings me one step closer to becoming a better backend developer. Looking forward to exploring more Node.js concepts in the coming days!


If you're also learning Node.js, I'd love to hear your thoughts or discuss backend concepts in the comments. Let's keep learning together! πŸš€

#NodeJS #JavaScript #BackendDevelopment #ExpressJS #WebDevelopment #RESTAPI #100DaysOfCode #LearningInPublic #Programming #DevCommunity

Top comments (0)