Creating Routes and Handling Requests with Express

In the previous blog, you created your first server using Node.js.
You used the built-in http module and handled a request manually.
It worked, but it was not very convenient.
As your application grows, writing everything using the raw http module becomes complex and repetitive.
This is where Express comes in.
What is Express.js?
Express.js is a minimal web framework built on top of Node.js.
It simplifies the process of building servers and handling requests.
Instead of manually managing low-level details, Express provides a clean and structured way to write backend code.
Why Express Simplifies Development
Let’s look at a simple comparison.
Using Node.js http module
const http = require("http");
const server = http.createServer((req, res) => {
if (req.url === "/" && req.method === "GET") {
res.end("Home Page");
}
});
server.listen(3000);
You have to manually check:
URL
HTTP method
Now imagine handling multiple routes. This quickly becomes messy.
Using Express
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Home Page");
});
app.listen(3000);
Much cleaner.
Express handles routing and request parsing for you.
Setting Up Express
First, install Express in your project:
npm init -y
npm install express
Now create a file:
app.js
Creating Your First Express Server
const express = require("express");
const app = express();
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Run the server:
node app.js
Your Express server is now running.
Understanding Routing
Routing means defining how your server responds to different requests.
Each route is based on:
HTTP method (GET, POST, etc.)
URL path
You define routes like this:
app.get("/", (req, res) => {
res.send("Home Page");
});
Handling GET Requests
GET requests are used to retrieve data.
app.get("/users", (req, res) => {
res.send("List of users");
});
When a user visits /users, this function runs.
Handling POST Requests
POST requests are used to send data to the server.
app.post("/user", (req, res) => {
res.send("User created");
});
This route handles data submission.
Sending Responses
Express provides simple methods to send responses.
res.send("Simple response");
You can also send JSON:
res.json({
name: "Shikhar",
role: "Developer"
});
This is commonly used in APIs.
How It Works (Flow)
You can visualize Express like this:
Request → Route Match → Handler Function → Response
When a request comes in:
Express checks routes
finds a match
executes the handler
sends a response
Real Example
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Welcome to my API");
});
app.get("/users", (req, res) => {
res.json([{ name: "Amit" }, { name: "Neha" }]);
});
app.post("/user", (req, res) => {
res.send("User created successfully");
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Why This Matters
With Express, you:
avoid repetitive code
structure your application properly
handle multiple routes easily
build APIs faster
This is why almost every Node.js backend uses Express or a similar framework.
Final Thought
Express does not replace Node.js.
It builds on top of it and makes development easier.
Instead of focusing on low-level details, you focus on building features.
That is what makes it powerful.






