Web Frameworks and the Request-Response Cycle

Beginner · 60 min

What actually happens inside a web framework between an HTTP request arriving and a response going out — routing, middleware, controllers, and MVC.

Before thisChoosing a Backend Language and Runtime

Sriniously · 60 min

Web Frameworks and the Request-Response Cycle

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by SriniouslyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A web framework exists to turn "an HTTP request arrived" into "the right function ran with the right data," so you don't hand-parse URLs and method verbs for every endpoint yourself.
  • Routing matches a request's method and path to a specific handler; middleware is a chain of functions that each get a chance to inspect, modify, or reject the request before it reaches that handler.
  • A controller (or handler)'s job is narrow by design — pull data out of the request, validate it, call into business logic, and shape a response — not to contain the business logic itself.
  • Whether a handler renders an HTML template or serializes JSON is a decision about who the response is for (a browser rendering a page vs. another program consuming data), not a difference in how routing or middleware work underneath.
  • MVC is less a rule every framework enforces and more a recurring shape — separate "what the data is," "what the user sees," and "what coordinates between them" — that shows up, loosely, in almost every web framework you'll touch.

What a framework is actually doing for you

Strip a web framework away and you’re left with a raw socket receiving bytes. Somebody still has to parse those bytes into a method, a path, a set of headers, and a body; figure out which piece of your code is supposed to handle a POST to /orders/42/cancel; run whatever cross-cutting checks apply (is this request authenticated? is it logging-worthy?); run your actual code; and turn whatever that code returns back into properly formatted HTTP bytes going the other direction. A framework’s entire reason for existing is doing all of that so you only have to write the one piece that’s actually specific to your app: what happens when someone cancels order 42.

That whole pipeline — request in, response out — is the request-response cycle, and it has a fairly consistent shape across frameworks, languages, and decades, even though the exact names change. If you want the raw mechanics of an HTTP message itself — status codes, headers, the request line — that ground belongs to DNS and HTTP; this lesson picks up from there and stays at the level of what your framework does with a request once it has one.

Routing: matching a request to code

The first thing that happens is routing. A router holds a table of patterns — GET /users/:id, POST /orders, DELETE /orders/:id/cancel — each pointing at a specific function. When a request comes in, the router checks its method and path against that table and picks the one that matches, extracting any path parameters (:id) along the way. Get the route table wrong — register a catch-all before a specific path, for instance — and requests silently get handled by the wrong code, which is why route order matters in most frameworks even though it rarely looks like it should.

Middleware: the chain before your code runs

Almost nothing you want to do with every request belongs inside every individual handler. You don’t want to copy-paste “check if the user is logged in” into fifty different functions. Middleware solves this: a chain of functions that each run in sequence before the matched handler does, where each one can inspect the request, modify it, log something, or — critically — stop the chain entirely and send a response early.

This is exactly how authentication middleware works: it checks for a valid session or token before anything else runs, and if that check fails, it returns a 401 immediately, and the handler that would have processed the actual request never executes at all. Order matters here too — a middleware that depends on authentication having already happened needs to sit after the auth middleware in the chain, not before it.

Controllers and handlers: the narrow translation layer

Once a request survives routing and middleware, it reaches a controller or handler function. Its job is intentionally narrow: pull the data it needs out of the request (path parameters, query string, JSON body), validate that data is shaped the way it expects, call into whatever business logic actually does the work, and translate the result back into an HTTP response with the right status code.

Notice what’s missing from that list: the controller isn’t where the business logic itself lives. A controller function that directly contains pricing rules or inventory checks is now impossible to call from anywhere except an HTTP request — not from a background job, not from a test without faking an entire request object, not from a CLI tool doing the same operation. Keeping that logic in a separate layer the controller just calls into is what makes it reusable and testable independent of HTTP entirely. How far you take that separation — a formal service layer, a repository layer for data access, explicit dependency injection between them — is genuinely an architectural decision with its own depth, and that’s deliberately out of scope here: see architectural patterns and dependency injection for that layer of the decision. This lesson stops at “what happens to a request inside your framework,” not “how should you structure the code that handles it.”

Templating vs. API-only responses

The last step is producing a response body, and frameworks generally support two shapes. A templating response takes data and a template file (HTML with placeholders) and renders them together into a complete HTML page — this is what you want when a browser is going to display the result directly to a human. An API-only response serializes data (almost always to JSON) and sends that instead, for a consumer that’s going to parse it programmatically: a frontend’s JavaScript making a fetch call, a mobile app, another backend service.

The routing and middleware machinery underneath doesn’t change based on this choice — the same router, the same auth middleware, the same controller pattern applies either way. What changes is only the last step: do you hand back markup meant for a human to look at, or data meant for a program to consume? Plenty of real systems do both from the same framework, serving rendered pages for some routes and a JSON API for others.

MVC: a shape, not a law

Model-View-Controller is the name usually given to this overall organization: a Model holding data and the rules around it, a View responsible for presentation, and a Controller coordinating between the two in response to a request. Almost every web framework borrows some version of this split, but none of them implement it identically — a JSON API framework might have no “View” in the template-rendering sense at all, just a serializer. Treat MVC as a recognizable shape you’ll see variations of everywhere, not a spec any framework is strictly compliant with. Once you can see routing, middleware, controllers, and the response-shape decision clearly, you’ll recognize this same cycle whether you’re reading Express, Django, Rails, or ASP.NET Core — the names differ, the shape doesn’t.

Check yourself

  1. 01What problem does a framework's router actually solve?

    It matches an incoming request's HTTP method and URL path against a list of registered patterns and calls the one specific function responsible for that combination, instead of making you manually inspect the raw request and branch on it yourself.

  2. 02What can a piece of middleware do that a route handler further down the chain cannot easily undo?

    It can end the request early — returning a response (like a 401 Unauthorized) before the request ever reaches the intended handler — which is why middleware like authentication checks runs first in the chain, upstream of everything that assumes a valid, authenticated request.

  3. 03Why shouldn't business logic live inside a controller function?

    Because a controller's job is translating between HTTP concepts (request bodies, status codes, headers) and your application's actual logic — mixing the two makes the logic hard to test without spinning up a fake HTTP request, and hard to reuse from anywhere that isn't a web request, like a background job or a CLI script.

  4. 04What's the real difference between a framework returning a rendered HTML page versus a JSON API response?

    It's about who's consuming the response — a browser that will display the HTML directly, versus another program (a frontend's JavaScript, a mobile app, another service) that will parse the JSON and decide what to do with it. The routing and middleware machinery underneath is the same either way; only the last step, producing the response body, differs.

  5. 05Is MVC a strict rule that frameworks follow exactly?

    No — it's a recurring organizational pattern (separate data, presentation, and coordination logic) that most frameworks bend to fit their own idioms, so you'll recognize the shape everywhere without finding two frameworks that implement it identically.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark