Skip to main content

Command Palette

Search for a command to run...

Creating Routes and Handling Requests with Express

Updated
3 min readView as Markdown

When you start building backend applications with Node.js, you’ll quickly realize that handling routes, requests, and responses manually can become repetitive and messy.

That’s where Express.js comes in.


📌 What is Express.js?

Express.js is a minimal and flexible web framework for Node.js that provides a structured way to build web applications and APIs.

Instead of writing everything from scratch using Node’s core modules, Express gives you:

  • Clean routing

  • Middleware support

  • Simplified request/response handling


🤔 Why Express Simplifies Node.js Development

Using the built-in http module in Node.js is powerful—but verbose.

🔴 Raw Node.js Example

const http = require("http");

const server = http.createServer((req, res) => {
  if (req.url === "/" && req.method === "GET") {
    res.end("Hello World");
  }
});

server.listen(3000);

Problems:

  • Manual routing logic

  • Hard to scale

  • Poor readability


🟢 Express.js Equivalent

const express = require("express");
const app = express();

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

app.listen(3000);

Much cleaner, right?

👉 Express abstracts away boilerplate so you can focus on logic instead of plumbing.


⚙️ Creating Your First Express Server

Step 1: Install Express

npm init -y
npm install express

Step 2: Create Server

const express = require("express");
const app = express();

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

🌐 Handling GET Requests

GET requests are used to fetch data.

app.get("/", (req, res) => {
  res.send("Welcome to my server");
});

You can also use route paths:

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

📦 Handling POST Requests

POST requests are used to send data to the server.

First, enable JSON parsing middleware:

app.use(express.json());

Now handle POST:

app.post("/data", (req, res) => {
  const userData = req.body;
  res.send(`Received: ${JSON.stringify(userData)}`);
});

📤 Sending Responses

Express provides multiple response methods:

res.send("Text response");
res.json({ message: "JSON response" });
res.status(200).send("Status set");

These make response handling far more intuitive compared to raw Node.js.


🧭 Understanding Routing (Core Concept)

Routing means defining how your server responds to different endpoints.

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

app.post("/user", (req, res) => {
  res.send("Create user");
});

👉 Same route, different methods → different behavior.


🧠 Key Takeaways

  • Express.js is a lightweight abstraction over Node.js

  • It simplifies:

    • Routing

    • Request handling

    • Response formatting

  • Makes your backend:

    • Cleaner

    • Scalable

    • Easier to maintain


🔚 Final Thoughts

If you're serious about backend development, learning Express.js is non-negotiable. It’s the foundation for building APIs, full-stack apps, and scalable systems in Node.js.

Once you're comfortable with this, the next step is:

  • Middleware

  • Routing architecture

  • REST API design

1 views