Hey Codders! 👋
It’s me, Vikas Sankhla from Web Codder. Today, we’re building something cool and real—our very own web server using Node.js! 🤩
No worries if this sounds hard. I’ll guide you step by step with easy words, fun code, and visuals. 🛠️
🚀 What Is a Server Anyway?
A server is like a shop 🏪.
- You (the user) come in and ask for something (like a toy 🚂).
- The shop gives you what you asked for (the toy).
A web server does the same!
You ask for a webpage, and it gives you the page. 📄
🛠️ Setting Up a Basic HTTP Server
In Node.js, we use the http
module to create a server.
Step 1️⃣: Create a server.js
File
Open your code editor and make a new file called server.js
. ✍️
Step 2️⃣: Add This Code
jsCopyEditconst http = require('http');
const server = http.createServer((req, res) => {
res.write('Hello from Web Codder! 🚀');
res.end();
});
server.listen(3000, () => {
console.log('Server is running at http://localhost:3000');
});

✅ What’s Happening?
http.createServer()
→ We create the server.(req, res) => {}
→ We set up what the server does when someone visits.server.listen(3000)
→ The server listens on port 3000 (like a door 🚪 number).
🔗 Test Your Server
1️⃣ Open your terminal.
2️⃣ Run:
bashCopyEditnode server.js
3️⃣ Go to your browser and type:
👉 http://localhost:3000
🎉 You’ll see: “Hello from Web Codder! 🚀”
📦 Handling Routes (GET, POST, etc.)
What if we want to handle different URLs?
Example:
/
→ Home page 🏠/about
→ About page ℹ️
📝 Updated Code:
jsCopyEditconst http = require('http');
const server = http.createServer((req, res) => {
if (req.url === '/') {
res.write('Welcome to the Home Page! 🏠');
} else if (req.url === '/about') {
res.write('This is the About Page. ℹ️');
} else {
res.write('404 Not Found 🚫');
}
res.end();
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000');
});
📮 Handling Different HTTP Methods
Let’s say someone sends data to your server. 🚚
We handle that with methods like GET, POST, PUT, DELETE.
Example: Only handle POST requests to /submit
.
✍️ Code:
jsCopyEditconst http = require('http');
const server = http.createServer((req, res) => {
if (req.url === '/submit' && req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
res.write('Data received: ' + body);
res.end();
});
} else {
res.write('Welcome! 😊');
res.end();
}
});
server.listen(3000, () => {
console.log('Server is listening on http://localhost:3000');
});

📬 Request & Response Objects
Let’s look closer at req and res:
Object | What It Does |
---|---|
req | Tells you what the user asked for (URL, method, data) |
res | Lets you send something back (HTML, text, data) |
✅ Quick Recap Table
Concept | Example |
---|---|
Create server | http.createServer() |
Handle routes | if (req.url === '/about') { ... } |
GET request | Browser asking for a page |
POST request | Form sending data to the server |
req & res objects | req.method , req.url , res.write() , res.end() |
🎯 Conclusion
And that’s it, Codders! 🎉 You now know how to:
- ✅ Build a basic HTTP server
- ✅ Handle routes
- ✅ Work with GET & POST requests
- ✅ Understand req & res objects
Isn’t that awesome? 🌟
👉 Keep Learning with Us:
- 🎥 Subscribe on YouTube: Web Codder
- 📸 Follow us on Instagram: @web_codder_official
- 💬 Join our WhatsApp group: Click here