Creating Routes and Handling Requests with Express.js

When building backend applications with Node.js, handling routes and requests using the built-in HTTP module can quickly become complex.
This is where Express.js makes everything simple.
In this blog, we’ll learn how to create routes and handle requests using Express in a clean and efficient way.
What is Express.js?
Express.js is a lightweight web framework for Node.js.
It helps you:
Handle routes easily
Manage requests and responses
Build APIs faster
In short, Express simplifies backend development by reducing boilerplate code.
Why Express Simplifies Node.js Development
Let’s compare raw Node.js vs Express.js.
❌ Without Express (Raw Node.js)
const http = require('http');
const server = http.createServer((req, res) => {
if (req.url === '/users' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: "Get users" }));
}
});
server.listen(3000);
Problems:
Manual routing logic
Hard to scale
Messy code for multiple routes
With Express.js
const express = require('express');
const app = express();
app.get('/users', (req, res) => {
res.json({ message: "Get users" });
});
app.listen(3000);
Benefits:
Clean and readable
Easy routing
Less code
Creating Your First Express Server
Step 1: Initialize project
npm init -y
Step 2: Install Express
npm install express
Step 3: Create server
import express from 'express'
const app = express();
app.listen(3000, () => {
console.log("Server running on port 3000");
});
How Routing Works in Express
Express matches:
URL (route)
HTTP method
Then calls the corresponding handler function.
Handling GET Requests
GET is used to retrieve data.
app.get('/users', (req, res) => {
res.status(200).json({
message: "List of users"
});
});
When you open /users, this route gets executed.
Handling POST Requests
POST is used to send data to the server.
app.use(express.json());
app.post('/users', (req, res) => {
const user = req.body;
res.status(201).json({
message: "User created",
data: user
});
});
req.bodycontains data sent by the clientWe return a response with status 201 Created
Sending Responses in Express
Express provides simple methods to send responses:
res.send()→ Send plain responseres.json()→ Send JSON datares.status()→ Set HTTP status code
Example:
app.get('/', (req, res) => {
res.status(200).send("Welcome to Express server");
});
Understanding Routing Structure
Express uses a simple pattern:
app.METHOD(PATH, HANDLER)
Example:
app.get('/products', handler);
app.post('/products', handler);
app.put('/products/:id', handler);
app.delete('/products/:id', handler);
This structure makes your code:
Predictable
Scalable
Easy to maintain
Conclusion
Express.js is one of the most powerful tools for building backend applications with Node.js.
By understanding routing and request handling, you can start building APIs and real-world applications efficiently.




