Creating Routes and Handling Requests with Express.js (Complete Beginner Guide)

i am learner whatever i will learn i will write here
Introduction
When building backend applications, handling requests and defining routes is one of the most important tasks.
Using Express.js, this becomes extremely simple and clean compared to raw Node.js.
In this guide, you’ll learn:
What Express.js is
Why it simplifies development
How to create a server
Handling GET & POST requests
Sending responses
Understanding routing clearly
What is Express.js?
Express.js is a minimal and flexible web framework for Node.js
It helps you:
Create servers easily
Define routes cleanly
Handle requests & responses
Build APIs faster
Why Express is Better than Raw Node.js
Let’s compare
Raw Node.js Server
import http from "http";
const server = http.createServer((req, res) => {
if (req.url === "/") {
res.write("Home Page");
res.end();
}
});
server.listen(3000);
Problems:
Manual routing
Hard to scale
Messy code
Express Server
import express from "express";
const app = express();
app.get("/", (req, res) => {
res.send("Home Page");
});
app.listen(3000);
Benefits:
Clean syntax
Easy routing
Scalable
Creating Your First Express Server
Step 1: Install Express
npm install express
Step 2: Create Server
import express from "express";
const app = express();
app.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});
Request → Route → Response Flow
Flow:
Client Request → Route Handler → Response
Understanding Routing in Express
Routing means:
Defining how your server responds to different URLs
Basic Route Syntax
app.METHOD(PATH, HANDLER)
Example:
app.get("/about", (req, res) => {
res.send("About Page");
});
Handling GET Requests
GET is used to fetch data
app.get("/users", (req, res) => {
res.send("List of users");
});
Open:
http://localhost:3000/users
Handling POST Requests
POST is used to send data
Step 1: Enable JSON Middleware
app.use(express.json());
Step 2: Create POST Route
app.post("/users", (req, res) => {
const user = req.body;
res.send(`User created: ${user.name}`);
});
Example Request Body
{
"name": "Ankit"
}
Sending Responses in Express
Express provides multiple response methods:
Send Text
res.send("Hello World");
Send JSON
res.json({ name: "Ankit", age: 22 });
Send Status Code
res.status(200).send("Success");
Multiple Routes Example
app.get("/", (req, res) => {
res.send("Home Page");
});
app.get("/about", (req, res) => {
res.send("About Page");
});
app.post("/login", (req, res) => {
res.send("Login Successful");
});
Understanding req and res
req (Request):
Contains incoming data
URL, body, headers
res (Response):
Used to send data back
Controls output
Real-World Example (Mini API)
const users = [];
app.post("/users", (req, res) => {
users.push(req.body);
res.json(users);
});
app.get("/users", (req, res) => {
res.json(users);
});
Common Beginner Mistakes
Forgetting
express.json()req.bodywill be undefinedWrong route method
Using GET instead of POST
Not sending response
Request hangs
Best Practices
Keep routes simple
Use meaningful paths
Separate routes into files (for large apps)
Always send a response
Easy Analogy
Think of Express like a restaurant
Customer = Request
Menu path = Route
Chef = Handler
Food = Response
Customer orders → Chef prepares → Food served
Conclusion
Now you understand:
✔ What Express.js is
✔ Why it simplifies Node.js
✔ Creating server
✔ Handling GET & POST requests
✔ Sending responses
✔ Routing concept clearly






