Servers & Web Frameworks
What a web framework like Express does for you, compared with the repetitive plumbing you'd otherwise write by hand.
What is it?
A web server has one core job: listen for connections, read each request, and send back a response. Node.js can do this with its built-in http module. But used directly, everything is your job: working out the URL and HTTP method, parsing the request body, and handling errors so one bad request doesn't crash the server.
A web framework is a library that already solves this plumbing. Express is the most common one for Node.js. It lets you say "when a GET request comes in for /users, run this function" and handles the request parsing, so you can focus on what your app does.
Explain like I'm 10
Writing a server with raw Node.js is like building a house by mining your own ore to forge nails. A framework like Express is a stocked hardware store: the nails, screws and pre-cut lumber already exist, so you spend your time designing the house.
Examples
Raw Node.js — doing everything yourself
const http = require("http");
const server = http.createServer((req, res) => {
if (req.method === "GET" && req.url === "/hello") {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello!");
} else {
res.writeHead(404);
res.end("Not found");
}
});
server.listen(3000);You must check the method and URL, set status codes and headers, and handle every unmatched path yourself.
The same server with Express
const express = require("express");
const app = express();
app.get("/hello", (req, res) => {
res.send("Hello!");
});
app.listen(3000);Express matches the method and path for you, defaults to a 200 status and sensible headers, and returns a 404 for anything unmatched.
How it works
Express is built on top of Node's http module; it doesn't replace it. When a request arrives, Express finds the registered route whose method and path match. It then calls your function with req and res objects that have helper methods like res.json() and res.status().
Why does it exist?
Almost every backend needs the same scaffolding: routing, body parsing, consistent headers and error handling. Writing it from scratch each time is repetitive and error-prone. Frameworks solve it once, and make code across projects look familiar.
When to use it
Use a web framework for almost any real backend, even a small one, so you don't hand-roll routing and error handling. It's the default unless you have a specific reason not to.
When not to use it
For a tiny script that answers one fixed request (like a health check with no other logic), a full framework can be overkill. Node's http module or a minimal library may be enough.
Common mistakes
Thinking a framework replaces Node.js. It's built on top of it.
Assuming Express is the only option. Fastify, Koa and NestJS solve the same problem with different trade-offs.
Forgetting that a framework still runs your code. It organizes things, but you still write the business logic.
Practice exercises
- Easy:
Write a raw Node.js
httpserver that responds with "pong" to any request. - Medium:
Rewrite that same server using Express, and add a second route that returns JSON.
- Hard:
List three things Express does automatically that you'd otherwise write by hand in raw Node.js.
Interview questions
What problem does a web framework solve?
It handles the repetitive low-level plumbing of a server (routing, parsing request data, response helpers, error handling), so developers can focus on app logic instead of rebuilding it each project.
Is Express a replacement for Node.js?
No. Express is a library on top of Node's built-in http module. It simplifies raw requests and responses, but Node still creates the server and handles connections.
Name one thing you'd have to write manually with raw Node.js that a framework provides out of the box.
Routing requests by method and path. In raw Node you check req.method and req.url with if-statements; a framework lets you write app.get("/path", handler).
What does `app.listen(3000)` in Express correspond to under the hood in raw Node.js?
It uses the same mechanism as http.createServer(...).listen(3000). Express builds its app on Node's http server rather than replacing it.
Why does raw `http.createServer` funnel every request through a single callback function, and what problem does that create as an app grows?
Node's http module is low-level: it gives you one entry point and you branch on method and URL yourself. As endpoints grow, that one function fills with if-statements and gets hard to read and maintain.
In raw Node.js, what happens to a request for a URL you never explicitly checked with an if-statement?
It falls into whatever else branch you wrote, if any. If you didn't handle it, the response may hang, because Node has no automatic "not found" behavior.
Compare `res.send("Hello!")` in Express to what you'd write manually with raw `http` to send the same response.
With raw http you call res.writeHead(200, { "Content-Type": "text/plain" }) then res.end("Hello!"). res.send() picks a sensible status and content type and ends the response for you.
Why is it wrong to think of Express and Node.js as two competing ways to build a server?
Express uses Node's own http module internally, so using Express still means using Node. You're only choosing between Node's low-level API and Express's higher-level layer.
Where do the `req` and `res` objects in an Express handler actually come from?
From Node's http module. Express wraps and extends them with helpers like res.json() and res.status(); it doesn't replace them.
Name two other Node.js web frameworks besides Express, and what does having multiple options tell you about framework choice?
Fastify and Koa (also NestJS). Having several shows there's no single "correct" choice: each makes different trade-offs (performance, structure, minimalism) while solving the same routing and request-handling problem.
A raw Node http server needs a 10th route added by hand. What specifically gets harder as each new `if` branch is added?
The single handler grows with every route. It gets harder to see which condition matches which URL, easier to get check order wrong, and harder for several developers to work on routes without editing the same function.
Why does Express automatically respond with 404 to an unmatched route, when raw `http.createServer` does not do this automatically?
"No route matched" is so common that Express handles it once, in its routing system, so no project has to write that catch-all by hand.
What does it mean that "a framework still runs your own code"? What decisions does it not make for you?
The framework handles structure (matching requests to handlers, formatting responses), but it doesn't know what your app should do. The business logic in each handler (what to fetch, compute, or enforce) is still yours.
When would using raw Node's http module instead of a framework actually make sense?
For something tiny and fixed, like one health-check endpoint with no other logic. A full framework's routing and middleware is more overhead than a few lines of raw Node.
If you removed the `else` branch from a raw Node http server that checks `req.method === "GET" && req.url === "/hello"`, what would happen to an unmatched request?
No response would be written or ended, so the client would hang indefinitely. Node sends no default response, unlike a framework's built-in 404.
Why do frameworks provide convenience methods like `res.json()` instead of just handing you Node's raw response object?
Sending JSON by hand means setting Content-Type, calling JSON.stringify, and ending the response in the right order every time. res.json() does it in one call, so you can't forget a step.
What's a concrete cost of pulling in a full framework for a project that only ever needs one fixed health-check endpoint?
You add a dependency (plus its own dependencies) to install, update and trust, along with some startup overhead and unused features, when a few lines of raw Node would do the job.
Why is raw Node http described as "mining your own ore to forge nails" compared to a framework?
With raw Node, even basic pieces everyone needs (routing, headers, error fallbacks) must be built from scratch. A framework supplies them so you can focus on the app.
If you swapped Express for Fastify in a project, what would change, and what would stay solved either way?
The API for defining routes and middleware would change, since each framework has its own syntax. The core problem (routing requests without hand-rolled plumbing) stays solved either way.
What's the risk of a team writing its own ad hoc routing and parsing logic instead of adopting an existing, widely used framework?
They'd re-solve problems (routing, error handling, header parsing) that established frameworks have already battle-tested. That costs time better spent on features and likely brings edge-case bugs those frameworks fixed long ago.