๐ Day 6: Creating a Basic HTTP Server using Node.js (Without Express)
๐จโ๐ซ What Youโll Learn Today
What is an HTTP server?
Why learn it without Express?
How to create a basic server using Node.js
Handling routes like
/,/about, and/contactSending HTML responses
Real-life example
FAQs to clear common doubts
๐ก Why Learn HTTP Server Without Express?
Even though Express is widely used, it is built on top of Node.jsโs native http module.
Understanding the basics helps you:
Learn how backend works under the hood
Build logic without any dependency
Prepare better for interviews
โ Once you understand this, using Express becomes even easier!
๐ฆ What is an HTTP Server?
An HTTP server listens for requests from the browser and sends back responses.
When you open http://localhost:3000, your browser sends a request. The server responds with a page or message.
๐ Step-by-Step: Create a Basic HTTP Server
๐ 1. Create a new folder
mkdir basic-http-server
cd basic-http-server
๐ 2. Create a file: server.js
const http = require('http'); // Core module
const fs = require('fs');
const path = require('path');
const PORT = 3000;
// Create server
const server = http.createServer((req, res) => {
// Get the requested URL
const url = req.url;
if (url === "/") {
res.writeHead(200, { "Content-Type": "text/html" });
res.write("<h2>Welcome to the Home Page</h2>");
res.end();
} else if (url === "/about") {
res.writeHead(200, { "Content-Type": "text/html" });
res.write("<h2>This is the About Page</h2>");
res.end();
} else if (url === "/contact") {
res.writeHead(200, { "Content-Type": "text/html" });
res.write("<h2>Contact us at: support@example.com</h2>");
res.end();
} else {
res.writeHead(404, { "Content-Type": "text/html" });
res.write("<h2>404 - Page Not Found</h2>");
res.end();
}
});
// Start the server
server.listen(PORT, () => {
console.log(`โ
Server is running on http://localhost:${PORT}`);
});
โถ Run the server
node server.js
๐ Open Browser:
http://localhost:3000/โ Home Pagehttp://localhost:3000/aboutโ About Pagehttp://localhost:3000/contactโ Contact Pagehttp://localhost:3000/xyzโ 404 Page
๐ง Real-Life Example: Serving an HTML File
Letโs say you want to serve a real HTML file instead of inline HTML.
Step 1: Create home.html
<!-- home.html -->
<!DOCTYPE html>
<html>
<head>
<title>Node Server</title>
</head>
<body>
<h1>Hello from Node.js Server</h1>
<p>This page is served using the HTTP module.</p>
</body>
</html>
Step 2: Update server.js
if (url === "/") {
const filePath = path.join(__dirname, 'home.html');
fs.readFile(filePath, 'utf-8', (err, data) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/html" });
res.end("<h2>Server Error</h2>");
} else {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(data);
}
});
}
โ Now your browser will load a full HTML page served by your Node server!
๐ Students Learn:
| Concept | Skill Developed |
Core module: http | Learn to handle requests and responses manually |
| Routing logic | Understand how URLs map to responses |
| Serving HTML | First step toward full-stack development |
| Node.js file handling | Use fs.readFile to serve files |
| Real server setup | See actual output in browser without frontend code |
โ FAQs โ Common Doubts
1) Whatโs the difference between Express and HTTP module?
| Feature | Native http | Express.js |
| Speed to write | Slower, more manual | Fast, easy syntax |
| Routing | Manual (if checks) | Built-in .get() etc. |
| Performance | Raw power | Slightly heavier |
Express is built on top of
httpand offers more features. But learninghttpfirst helps you build strong fundamentals.
2) Do we need to install any packages to use http module?
โ No, itโs a built-in core module in Node.js. Just use:
const http = require('http');
3) Can we return CSS, JS, and image files too from HTTP server?
โ Yes. But youโll need to check the request URL, detect the file type, and set proper content-type like:
res.writeHead(200, { "Content-Type": "text/css" });
๐ง This can become complex โ thatโs why frameworks like Express are preferred.
4) Why is fs.readFile used in the server?
To read external files (like HTML) and serve them to the browser.
This is how backend servers send HTML to the frontend.
๐ Practice Tasks for Students
Create
/about.htmland/contact.htmlpages and serve them.Handle the
/servicesroute.Return 404 page with a custom HTML message.
Style your HTML with inline CSS or serve a CSS file (challenge).
โ Summary
| What You Built | What You Learned |
| A basic HTTP server | Sending browser responses using Node.js |
Routing with if logic | Handling multiple URLs |
| Serve external HTML | Using fs.readFile and path |
| Real app on localhost | Output visible in browser & console |
๐ Now youโve built your first complete backend server using pure Node.js!
Would you like to move on to Day 7: Intro to Express.js and setting up the first route in the same structured style next?
๐ Stay Connected
If you found this article helpful and want to receive more such beginner-friendly and industry-relevant Node JS notes, tutorials, and project ideas โ ๐ฉ Subscribe to our newsletter by entering your email below.
And if you're someone who wants to prepare for tech interviews while having a little fun and entertainment, ๐ฅ Donโt forget to subscribe to my YouTube channel โ Knowledge Factory 22 โ for regular content on tech concepts, career tips, and coding insights!
Stay curious. Keep building. ๐
About the author
- Hi there, tech enthusiasts! I'm a passionate Software Developer driven by a love for continuous learning and innovation. I thrive on exploring new tools and technologies, pushing boundaries, and finding creative solutions to complex problems. What You'll Find Here On my Hashnode blog, I share: ๐ In-depth explorations of emerging technologies ๐ก Practical tutorials and how-to guides ๐งInsights on software development best practices ๐Reviews of the latest tools and frameworks ๐ก Personal experiences from real-world projects. Join me as we bridge imagination and implementation in the tech world. Whether you're a seasoned pro or just starting out, there's always something new to discover! Letโs connect and grow together! ๐
Comments
Join the discussionNo comments yet. Be the first to comment.
Node.js Mastery: From Basics to Real-world Projects
Learn Node.js from basics to advanced with Express, REST APIs, MongoDB, and real projects. Perfect for beginners, frontend devs, and backend aspirantsโsimple language, real-world examples.

