Stop 05 · Express.js & Web FrameworksTopics · 1/14

Express Basics

Part of the Backend Developer Roadmap

In short

Node's built-in http module can run a server, but you end up parsing URLs and bodies by hand. Express gives you a clean way to say: for this request, send that response.

A minimal, unopinionated web framework for Node.js that handles routing, middleware and HTTP plumbing — the most widely used framework for building Node APIs.

The mascot at a dhaba counter takes an order slip and slides a plate across.

Think of it like this

The dhaba counter

At a highway dhaba, you say 'two parathas' at the counter and a plate comes back. The cook doesn't care who you are, only what you ordered. An Express app is that counter: a request comes in, your handler cooks, a response goes out.

Watch it happen

One request, one response

Client
Express app
Handler

GET /health

1/5A browser or Postman sends a GET request to your server on port 3000.

The big ideas

What you need to know

app is your server

const app = express() creates it. app.listen(3000) starts accepting requests on that port.

Method + path = route

app.get("/health", handler) runs only for GET requests to /health. POST to the same path won't match.

JSON in, JSON out

app.use(express.json()) parses JSON bodies into req.body. res.json() sends objects back as JSON.

Always respond once

Every request must end with exactly one res.send or res.json. Forget it and the client hangs.

See it in code

Walk through the code

js
1import express from "express";2 3const app = express();4app.use(express.json());5 6app.get("/health", (req, res) => {7  res.json({ ok: true, time: new Date().toISOString() });8});9 10app.post("/echo", (req, res) => {11  res.status(201).json({ youSent: req.body });12});13 14const PORT = process.env.PORT ?? 3000;15app.listen(PORT, () => console.log(`API on http://localhost:${PORT}`));

1/4Create the app and turn on JSON body parsing for every request.

Install with npm i express, add "type": "module" to package.json, and run node index.js. Try both routes with curl or Postman.

Save yourself the pain

Common mistakes

Oops

Forgetting app.use(express.json()) and getting req.body as undefined.

Do this instead

Add the JSON parser before your routes. Without it, Express doesn't read the request body at all.

Oops

Hard-coding port 3000, then the deploy platform can't reach your app.

Do this instead

Use process.env.PORT with a fallback. Hosting platforms tell your app which port to use.

Your mission

Your first Express API

~20 min

Quick check

You send a POST with a JSON body, but req.body is undefined. What's the most likely cause?

Go deeper

  1. 1Build a 'Hello World' Express server with a single route.
  2. 2Compare how much boilerplate this saves versus the raw `http` module from Phase 2.
  3. 3Read Express's own docs guide once, start to finish — it's short and worth it.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge