Skip to main content

Command Palette

Search for a command to run...

Creating Routes and Handling Requests with Express

Understanding Express routing, requests, responses, and server handling basics.

Published
•5 min read
Creating Routes and Handling Requests with Express
G
I enjoy blending technology with business to build solutions that create real impact. I’m fascinated by how technology empowers people, businesses, and startups. I believe thoughtful use of technology quietly improves lives, and that belief fuels everything I build and explore 💻.

If you’ve started backend development with Node.js recently, chances are you’ve already heard people saying that Express.js makes backend development much easier. Initially, this may feel slightly exaggerated because Node.js itself can already create servers using the built-in HTTP module. But the moment you start building routes, handling requests, managing responses, or scaling APIs, the difference between raw Node.js and Express.js becomes very noticeable.

Express.js is one of the most widely used backend frameworks in the Node.js ecosystem because it simplifies server creation and routing significantly. Instead of manually handling low-level request logic repeatedly, Express provides a cleaner and more structured way of building backend applications.

In this blog, we’ll understand what Express.js actually is, why developers prefer it over raw Node.js servers, how routing works, how GET and POST requests are handled, and how responses are sent back from the server.

What Exactly is Express.js?

Express.js is a lightweight web framework built on top of Node.js. It simplifies backend development by providing features like routing, middleware handling, request processing, and response management.

Without Express, backend development using only Node.js becomes more repetitive because developers need to manually handle routing logic, headers, request parsing, and many low-level operations.

Installation:

npm install express

Basic setup:

const express = require("express");

const app = express();

Here:

  • express() creates the Express application

  • app becomes the main server object used for routes and middleware

Why Express Simplifies Node.js Development

One of the biggest reasons Express became popular is that it removes a lot of repetitive backend boilerplate code.

For example, creating a server using raw Node.js HTTP module looks like this:

const http = require("http");

const server = http.createServer((req, res) => {
  res.end("Hello World");
});

server.listen(3000);

This works perfectly fine, but once multiple routes, methods, and request handling logic start increasing, the code quickly becomes harder to manage.

Express simplifies the same thing heavily.

Example:

const express = require("express");

const app = express();

app.get("/", (req, res) => {
  res.send("Hello World");
});

app.listen(3000);

This is cleaner, more readable, and easier to scale later.

Creating Your First Express Server

Creating a basic Express server is very straightforward.

Example:

const express = require("express");

const app = express();

app.listen(3000, () => {
  console.log("Server running");
});

Here:

  • app.listen() starts the server

  • 3000 is the port number

  • Callback executes after the server starts successfully

Once the server starts, it begins listening for incoming client requests.

Understanding Routes in Express

Routes are one of the core parts of Express.js.

A route defines how the server should respond when a client requests a particular URL using a specific HTTP method.

Basic route example:

app.get("/", (req, res) => {
  res.send("Home Route");
});

Here:

  • "/" is the route path

  • app.get() handles GET requests

  • Callback executes whenever the route is accessed

This callback function is called the route handler.

Understanding GET Requests

GET requests are mainly used for fetching data from the server.

Example:

app.get("/users", (req, res) => {
  res.send("All users fetched");
});

Whenever the client visits:

/users

the server sends the response back.

GET routes are heavily used for:

  • Fetching users

  • Retrieving products

  • Getting posts

  • Loading dashboards

  • Reading data from databases

Most frontend applications continuously send GET requests while displaying backend data.

Handling POST Requests

POST requests are mainly used for sending data to the server.

Example:

app.post("/register", (req, res) => {
  res.send("User registered");
});

Here:

  • app.post() handles POST requests

  • Client sends data to the backend

  • Backend processes the incoming request

POST requests are commonly used for:

  • Registration

  • Login

  • Creating posts

  • Uploading data

  • Submitting forms

Unlike GET requests, POST requests usually carry request body data.

Understanding Request and Response Objects

Every route handler in Express receives two important objects:

  • req

  • res

Example:

app.get("/", (req, res) => {
  res.send("Hello");
});

Here:

  • req contains incoming request information

  • res is used for sending responses back

The request object contains things like:

  • Headers

  • Query parameters

  • Route parameters

  • Request body

  • HTTP method

The response object controls what gets sent back to the client.

Sending Responses in Express

Express provides multiple methods for sending responses.

Basic text response:

res.send("Hello World");

JSON response:

res.json({
  name: "Gaurang"
});

Status code response:

res.status(200).send("Success");

These helper methods make Express much easier compared to manually handling headers and response streams in raw Node.js.

Route Parameters in Express

Express also supports dynamic routes using route parameters.

Example:

app.get("/users/:id", (req, res) => {
  res.send(req.params.id);
});

If the request becomes:

/users/101

Output:

101

This becomes extremely useful while building REST APIs involving users, products, posts, or database resources.

Why Routing Matters So Much

Routing becomes one of the most important parts of backend applications because it controls how requests move through the server.

Different routes usually handle different responsibilities.

For example:

/users
/products
/orders
/login
/register

Each route becomes responsible for a different backend operation.

Express makes managing these routes much cleaner and easier compared to manually checking request URLs and methods using raw Node.js.

Final Thoughts

Express.js became one of the most popular Node.js frameworks because it simplifies backend development heavily. Instead of manually handling low-level HTTP logic repeatedly, Express provides clean routing methods, request handling, response utilities, and structured server organization.

Once routing becomes clear, backend development starts feeling much more manageable because every incoming request follows a predictable flow through route handlers and responses. Whether it’s GET requests, POST requests, dynamic routes, or API creation, Express provides a clean and scalable foundation for modern backend applications.

Creating Routes and Handling Requests with Express