Creating Routes and Handling Requests with Express
After learning basic Node.js servers, one thing quickly becomes obvious:
Building everything manually using the built-in http module can become repetitive and messy.
Even simple things like:
handling routes
sending responses
managing requests
processing APIs
require writing a lot of code in raw Node.js.
This is exactly why Express.js was created.
Express is one of the most popular Node.js frameworks because it makes backend development:
cleaner
faster
easier to manage
In fact, most modern Node.js backend applications use Express in some form.
In this article, we’ll learn:
what Express.js is
why developers use it
how routing works
how to handle GET and POST requests
how responses are sent back to clients
By the end, you’ll have your first Express server running with proper routes.
What Is Express.js?
Express.js is a lightweight web framework built on top of Node.js.
It simplifies backend development by providing easy tools for:
routing
request handling
middleware
APIs
server management
Instead of manually handling low-level HTTP logic, Express gives developers a cleaner and simpler syntax.
Why Express Simplifies Node.js Development
Let’s compare raw Node.js vs Express.
Creating a Server Using Raw Node.js
const http = require("http");
const server = http.createServer((req, res) => {
if (req.url === "/") {
res.write("Home Page");
res.end();
}
});
server.listen(3000);
This works, but even a simple route requires manual handling.
As applications grow:
routes increase
conditions increase
code becomes difficult to manage
Same Server Using Express
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Home Page");
});
app.listen(3000);
Much cleaner and easier to read.
This simplicity is the biggest reason Express became popular.
Why Developers Love Express
Express helps developers:
create APIs faster
write cleaner routes
reduce boilerplate code
organize backend applications better
Instead of fighting low-level server handling, developers can focus on application logic.
Installing Express
Before using Express, it must be installed inside the project.
Run this command inside your project folder:
npm install express
This downloads Express into the project.
Understanding What npm Is Doing
npm installs packages from the Node.js ecosystem.
After installation:
Express code becomes available
Node.js can import it using
require()
Creating Your First Express Server
Create a file:
app.js
Now write:
const express = require("express");
const app = express();
app.listen(3000, () => {
console.log("Server running");
});
Run the server:
node app.js
Output:
Server running
The Express server is now active.
Understanding the Code Step-by-Step
Importing Express
const express = require("express");
This imports the Express package.
Creating the Express App
const app = express();
This creates the Express application object.
Most backend operations will now happen through app.
Starting the Server
app.listen(3000);
This starts the server on port 3000.
What Is Routing?
Routing means:
deciding what should happen when a user visits a particular URL.
Example:
//about/profile
Different URLs trigger different route handlers.
Understanding Route Handlers
A route handler is simply a function that runs when a specific request arrives.
Example:
app.get("/", (req, res) => {
res.send("Home Page");
});
Here:
"/"is the routethe function handles requests for that route
Request → Route Handler → Response Flow
This is the basic request flow inside Express.
Handling GET Requests
GET requests are mainly used for:
fetching data
opening pages
retrieving information
Example:
app.get("/about", (req, res) => {
res.send("About Page");
});
When the browser visits:
http://localhost:3000/about
the response becomes:
About Page
Understanding req and res
Every route handler receives two important objects:
| Object | Purpose |
|---|---|
req |
Incoming request information |
res |
Response sent back to client |
Request Object (req)
The request object contains:
URL
headers
parameters
query data
request body
Example:
app.get("/", (req, res) => {
console.log(req.url);
res.send("Hello");
});
Response Object (res)
The response object is used to send data back.
Example:
res.send("Hello World");
Sending Different Types of Responses
Express can send:
text
JSON
HTML
files
Sending Text
res.send("Hello");
Sending JSON
res.json({
name: "Ritesh",
age: 22
});
Express Routing Structure Visualization
graph TD
A[Express App]
A --> B[/ Route]
A --> C[/about Route]
A --> D[/users Route]
This visual shows how applications organize multiple routes.
Handling POST Requests
POST requests are mainly used for:
sending data
form submissions
creating records
login systems
Example:
app.post("/login", (req, res) => {
res.send("Login successful");
});
Difference Between GET and POST
| Method | Purpose |
|---|---|
| GET | Retrieve data |
| POST | Send data to server |
Why APIs Use Different HTTP Methods
Modern APIs use HTTP methods to define actions clearly.
Examples:
GET → fetch users
POST → create user
PUT → update user
DELETE → remove user
This structure improves API organization.
Understanding Browser Requests
When you type a URL in the browser:
- the browser sends a GET request automatically
POST requests usually happen through:
forms
frontend applications
API tools like Postman
Creating Multiple Routes
Example:
app.get("/", (req, res) => {
res.send("Home");
});
app.get("/about", (req, res) => {
res.send("About");
});
app.get("/contact", (req, res) => {
res.send("Contact");
});
Each route handles a different URL.
Why Express Became So Popular
Express became popular because it:
reduces boilerplate
simplifies routing
works perfectly with APIs
integrates well with frontend frameworks
It allows developers to build backend applications much faster than raw Node.js alone.
Real-World Use Cases of Express
Express is commonly used for:
REST APIs
authentication systems
admin panels
backend services
e-commerce backends
mobile app APIs
It became one of the standard backend tools in JavaScript development.
Common Beginner Mistakes
Forgetting to Start Server
app.listen(3000);
Without this, the server will not run.
Wrong Route Method
Using:
app.get()
instead of:
app.post()
for form submissions.
Port Already in Use
If port 3000 is occupied:
app.listen(3001);
Why Routing Matters So Much
Routing is the foundation of backend applications.
Almost every backend feature depends on routes:
authentication
APIs
dashboards
databases
user profiles
Understanding routing properly makes future Express concepts much easier.
What Comes Next After Routing
Once comfortable with:
routes
request handling
responses
the next important topic becomes:
middleware
route parameters
REST APIs
authentication
because real backend applications combine all these concepts together.
Assignment Practice
Install Express.
npm install express
Create a simple Express server.
const express = require("express");
const app = express();
app.listen(3000);
Create a GET route.
app.get("/", (req, res) => {
res.send("Home Page");
});
Create a POST route.
app.post("/login", (req, res) => {
res.send("Login Route");
});
Run the server and visit:
http://localhost:3000
Try creating additional routes and different responses to better understand how Express handles incoming requests and sends responses back to clients.