DEV Community

Bhupesh Chandra Joshi
Bhupesh Chandra Joshi

Posted on

Creating Routes and Handling Requests with Express (Without Melting Your Brain)

Ever tried building a backend with raw Node.js and felt like you were assembling IKEA furniture using only your teeth?

Yeah. Same.

Enter Express.js — the tiny but mighty web framework that hands you a box of tools, pats your head, and whispers:

"Relax, sweet child. I got you."

Let’s walk through what Express is, why developers can’t stop adopting it like stray puppies, and how to build your first server with actual routes that do actual things.

Your dopamine receptors may want to buckle up. 🚀

What the Heck Is Express.js?

Express.js is a minimalist, flexible web framework for Node.js. In human language:

It helps you build web servers without feeling like you're doing forbidden rituals with callbacks.

Think of Node’s native HTTP server as a raw potato.

Express is that potato—

✨ sliced

✨ seasoned

✨ air‑fried

✨ and served with garlic mayo

Why Express Makes Node.js Development a Whole Lot Less Spicy

Node’s built-in HTTP server is powerful… but also a gremlin.

With raw Node you must manually:

• Parse URLs

• Deal with JSON bodies

• Handle routing yourself (oh joy)

• Remember that res.end() exists, or your server will just stare at you silently forever

Express says:

• “I’ll parse that for you.”
• “I’ll route that for you.”
• “I’ll JSON that for you.”
• “I’ll hold your beverage.”

Small Comparison (a.k.a. the Emotional Damage Exhibit)

Raw Node.js HTTP server

``js
const http = require("http");

const server = http.createServer((req, res) => {
if (req.url === "/" && req.method === "GET") {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello from raw Node 😬");
} else {
res.writeHead(404);
res.end("Nope.");
}
});

server.listen(3000, () => console.log("Server running..."));
`

Same thing with Express

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

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

app.listen(3000);
`

The difference?

One looks like a therapy session waiting to happen.

Creating Your First Express Server

Let’s go from zero to “my server works and I’m amazing” in 10 seconds.

Install Express:


npm install express

Create a file called server.js:

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

app.listen(3000, () => {
console.log("🚀 Server is blasting off on http://localhost:3000");
});
`

Run:


node server.js

Boom. You’re officially an Express parent.

Handling GET Requests (a.k.a. “Give me stuff”)

GET is like the read-only friend who asks for things without contributing snacks.

js
app.get("/hello", (req, res) => {
res.send("Hi! You GET me 😌");
});

Open your browser at http://localhost:3000/hello

Your brain: 💥 Dopamine: ☀️

Handling POST Requests (a.k.a. “Here, take my data pls”)

POST is how clients send data.

But first: tell Express to read JSON bodies.

(It’s not psychic, unfortunately.)

js
app.use(express.json());

Now create a POST route:

js
app.post("/signup", (req, res) => {
const { username } = req.body;
res.send(Welcome aboard, ${username}! 🎉);
});

If you send:

json
{"username": "JavaScriptNinja"}

The server responds with pure, warm validation.

Sending Responses (Your Server’s Love Language)

Express gives multiple ways to respond:

• res.send() — sends strings, objects, or buffers
• res.json() — sends JSON
• res.status() — sets HTTP status

Example:

js
app.get("/status", (req, res) => {
res.status(200).json({ healthy: true });
});

Short and sweet — unlike your last sprint.

Understanding Routing (The Brain-Friendly Way)

Think of routing like a giant magical vending machine:

• You press a button (URL + method)
• A specific handler pops out


( Request )

[ Route Switchboard ]

( Handler Function )

( Response )

And routing structure usually looks like:


app.get("/cats", ...)
app.post("/cats", ...)
app.put("/cats/:id", ...)
app.delete("/cats/:id", ...)
`

Each route is a tiny worker who knows exactly what job to do.

No confusion. No tears. Just productivity.

Final Thoughts: Express = Sanity

Using Express is like switching from:

• a flip phone → to a smartphone

• washing dishes by hand → to owning a dishwasher

• spaghetti code → to “this actually makes sense now”

Express handles the boring parts so you can focus on the fun: building stuff that feels magical.

Top comments (0)