Hey there, awesome devs! π Have you ever wondered how you can build a web server without using frameworks like Express? π€ Well, guess what? Node.js has a built-in HTTP module that lets you create a fully functional web server in just a few lines of code! π‘
In this blog, weβre going to explore Node.jsβs HTTP module, how to create a simple server, and why itβs such a powerful tool. By the end, youβll be a pro at handling HTTP requests in Node.js! π
π What is the HTTP Module in Node.js?
The HTTP module in Node.js allows you to create a web server that can handle requests and send responsesβjust like a real web application! The best part? Itβs built into Node.js, so no extra installations are needed! β‘
β Why Use the HTTP Module?
- Lightweight β No need for extra dependencies.
- Fast β Handles thousands of requests efficiently.
- Fully Customizable β Control every aspect of request handling.
- Great for Learning β Helps understand how web servers work under the hood.
π Creating a Simple HTTP Server
Letβs jump straight into coding and create a basic web server! ποΈ
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, world! π');
});
server.listen(3000, () => {
console.log('Server running on http://localhost:3000 π');
});
β How it works:
- We import the HTTP module.
- We create a server using
http.createServer()
. - When a request comes in, we write a response with
res.end()
. - The server listens on port 3000.
Now, open your browser and go to http://localhost:3000
βyou should see βHello, world! πβ! π
π© Handling Different Routes
A real web server should respond differently depending on the URL (route). Letβs handle different paths:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
if (req.url === '/') {
res.end('Welcome to the Home Page! π ');
} else if (req.url === '/about') {
res.end('About Us Page π');
} else {
res.writeHead(404);
res.end('404 Not Found β');
}
});
server.listen(3000, () => {
console.log('Server running on http://localhost:3000 π');
});
β How it works:
- We check
req.url
to determine which page the user is requesting. - We respond with different messages for
/
,/about
, and a 404 error for unknown routes.
Try visiting:
-
http://localhost:3000/
π -
http://localhost:3000/about
π -
http://localhost:3000/contact
(Oops! 404 β)
π€ Sending HTML Responses
Instead of plain text, letβs send HTML responses:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Welcome to My Node.js Server! π</h1>');
});
server.listen(3000, () => {
console.log('Server running on http://localhost:3000 π');
});
β
Now, visiting http://localhost:3000
will display a styled HTML page! π₯οΈ
π‘ Handling JSON Responses (For APIs)
Letβs turn our server into a mini API that serves JSON data! π οΈ
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
const data = { message: 'Hello, this is JSON data! π' };
res.end(JSON.stringify(data));
});
server.listen(3000, () => {
console.log('Server running on http://localhost:3000 π');
});
β
Now, visiting http://localhost:3000
will return JSON dataβperfect for building APIs! π‘
π₯ Final Thoughts
The HTTP module in Node.js is powerful, lightweight, and easy to use. Whether youβre building a simple web server, handling API requests, or learning backend developmentβunderstanding the HTTP module is a must! π―
In the next article, weβll dive deeper into Creating a Node Serverβstay tuned! π₯
If you found this blog helpful, make sure to follow me on GitHub π github.com/sovannaro and drop a β. Your support keeps me motivated to create more awesome content! π
Happy coding! π»π₯
Top comments (0)