Creating Routes and Handling Requests with Express

Introduction
Once you understand how Node.js runs JavaScript and handles basic HTTP servers, the next step is making your code easier to manage. Writing everything using Node’s built-in http module quickly becomes complex. This is where Express.js comes in. It provides a cleaner and more structured way to handle routes, requests, and responses.
What is Express.js
Express.js is a minimal and flexible web framework built on top of Node.js. It does not replace Node, but simplifies how you build server-side applications. Instead of manually handling request parsing, routing, and response logic, Express gives you a structured way to do all of it.
In raw Node.js, you often check the URL and method manually inside a single function. As your application grows, this becomes difficult to manage. Express separates this logic into clear routes.
Why Express Simplifies Development
With Node’s http module, you write low-level logic. You manually inspect req.url, handle methods, and send responses. This leads to repetitive and harder-to-read code.
Express abstracts these details. It allows you to define routes like /users or /login and attach handlers directly. Instead of thinking about how to process requests, you focus on what each route should do.
The difference is not just fewer lines of code. It is about readability, maintainability, and scalability.
Creating Your First Express Server
First, install Express in your project:
npm init -y
npm install express
Create a file named server.js and write:
const express = require("express");
const app = express();
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Run the server:
node server.js
At this point, your server is running, but it does not respond to any routes yet.
Understanding Routing
Routing means deciding what should happen when a specific request comes to your server. Each route is defined by two things: the HTTP method and the URL path.
When a request comes in, Express matches it with the correct route and executes the associated handler function.
Handling GET Requests
A GET request is used to retrieve data. In Express, you define it like this:
app.get("/", (req, res) => {
res.send("Hello from Express");
});
When you open http://localhost:3000, this route runs and sends a response.
You can also create multiple routes:
app.get("/about", (req, res) => {
res.send("About page");
});
Each route is clearly separated, making the structure easy to understand.
Handling POST Requests
POST requests are used to send data to the server, usually from forms or APIs.
To handle POST requests, you also need middleware to parse incoming data:
app.use(express.json());
app.post("/user", (req, res) => {
const data = req.body;
res.send(`User received: ${JSON.stringify(data)}`);
});
Here, Express automatically parses JSON data and makes it available in req.body.
Sending Responses
Express provides simple methods to send responses. The most common one is res.send().
You can send text, JSON, or even HTML:
res.send("Simple text response");
res.json({ message: "JSON response" });
This flexibility makes it easy to build APIs and web applications.
Comparing Node HTTP vs Express
In a raw Node server, you typically write logic like this:
const http = require("http");
http.createServer((req, res) => {
if (req.url === "/" && req.method === "GET") {
res.end("Home");
}
}).listen(3000);
Everything is handled inside one place. As routes increase, this becomes messy.
With Express, each route is separated and clearly defined. The structure is easier to read and extend.
Request Flow in Express
Request → Route Match → Handler Function → Response
When a request arrives, Express checks all defined routes. Once a match is found, the corresponding function runs and sends a response.
Express Routing Structure Concept
Instead of one large function, Express organizes your server into multiple small route handlers. Each handler is responsible for a single path and method. This modular approach is what makes Express powerful.
Conclusion
Express transforms how you build backend applications with Node.js. It removes boilerplate, improves readability, and introduces a clear routing system. By understanding routes, request handling, and responses, you now have the foundation to build real-world APIs and applications.
The next step is learning middleware, route parameters, and connecting your server to databases.




