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
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");
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");
});
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.urlreq.methodreq.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");
}
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");
This tells the browser that the response contains JSON data.
Some common MIME types include:
application/jsontext/htmltext/plainimage/pngapplication/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. -
reqcontains request information. -
resis 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)