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! ๐ป๐ฅ