Module F-6·22 min read

The built-in http module, why Express exists, routing, middleware pipelines, route parameters, query strings, and sending JSON responses.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module F-6 — Building HTTP Servers and REST APIs with Express

What this module covers: Express is the most widely used Node.js web framework — and for good reason. It takes the built-in http module and adds routing, middleware, and a clean request/response API without imposing a rigid structure. This module covers everything you need to build a real REST API: routing, route parameters, query strings, request bodies, middleware pipelines, and error responses. By the end you will have a working multi-route API with proper HTTP semantics.


From Raw http to Express

You saw the built-in http module in F-1:

javascript

This works, but it has no routing. Every request — regardless of URL or method — hits the same handler. To build a real API you need to route GET /users to one handler, POST /users to another, DELETE /users/:id to a third. You also need to parse JSON bodies, read headers, set response codes properly, and handle errors consistently.

You could implement all of that yourself on top of http. Express already did it — and did it well.

bash

Version note: you're likely installing Express 5. npm install express has pulled Express 5.x since September 2024 — that's the latest tag now, not Express 4. This module's code works the same under both versions, but two differences are worth flagging up front so the rest of the module doesn't silently assume Express 4 behavior:

  • Async error forwarding. In Express 5, if an async route handler's promise rejects, Express automatically forwards that rejection to your error-handling middleware. You don't strictly need the manual try { ... } catch (err) { next(err) } pattern shown in the Error Handling section below — though it's still fine practice, and it is required if you're on Express 4 (still extremely common in existing production codebases). The Error Handling section explains why that pattern exists in the first place.
  • Stricter routing. Express 5 ships a new router (path-to-regexp v8) that rejects some Express 4-era path patterns — most notably bare wildcards like app.get('*', ...), which now throw at startup. Use a named wildcard like app.get('/*splat', ...) instead.

Run npm ls express if you're ever unsure which version a project is actually running.


Your First Express Server

javascript
bash

The Express API:

  • express() creates an application instance
  • app.use(...) adds middleware (runs on every request)
  • app.get(path, handler) registers a GET route
  • req — the incoming request object
  • res — the response object
  • res.json(data) — sends JSON with Content-Type: application/json and status 200

HTTP Methods and REST Conventions

REST APIs use HTTP methods to express intent:

MethodMeaningExample
GETRead a resourceGET /users — list all users
POSTCreate a resourcePOST /users — create a user
PUTReplace a resource entirelyPUT /users/42 — replace user 42
PATCHUpdate part of a resourcePATCH /users/42 — update user 42's email
DELETERemove a resourceDELETE /users/42 — delete user 42

Express has a method for each:

javascript

Route Parameters

Route parameters are named placeholders in the URL path, prefixed with :. Express captures them and puts them in req.params.

javascript
bash

Important: route parameters are always strings, even if the URL contains a number. Convert them explicitly:

javascript

Query Strings

Query strings are the ?key=value part of a URL. Express parses them automatically into req.query.

javascript
bash

Query string values are always strings — convert them as needed.


Request Bodies

For POST, PUT, and PATCH requests, data comes in the request body. With express.json() middleware registered, Express automatically parses JSON bodies into req.body.

javascript
bash

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.