0
0
Expressframework~10 mins

Controller pattern for route handlers in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Controller pattern for route handlers
Client sends HTTP request
Express Router receives request
Router calls Controller function
Controller processes request
Controller sends response
Client receives response
The client sends a request, Express router directs it to a controller function, which processes and sends back a response.
Execution Sample
Express
import express from 'express';
const app = express();

// Controller function
const getUser = (req, res) => {
  res.send(`User ID: ${req.params.id}`);
};

app.get('/user/:id', getUser);
This code sets up a route '/user/:id' that uses a controller function to send back the user ID from the URL.
Execution Table
StepActionInputController StateResponse Sent
1Client sends GET /user/42GET /user/42WaitingNo
2Express router matches route/user/42WaitingNo
3Router calls getUser controller/user/42Processing req.params.id = '42'No
4Controller executes res.send/user/42Processed id '42'Yes: 'User ID: 42'
5Response sent to clientUser ID: 42DoneYes
💡 Response sent, request cycle complete
Variable Tracker
VariableStartAfter Step 3After Step 4Final
req.params.idundefined'42''42''42'
responseSentfalsefalsetruetrue
Key Moments - 3 Insights
Why does the controller get called after the router matches the route?
Because the router directs matching requests to the controller function as shown in execution_table step 3.
What does res.send do inside the controller?
It sends the response back to the client and ends the request cycle, as seen in execution_table step 4.
Why is req.params.id available inside the controller?
Express parses the URL parameters before calling the controller, so req.params.id holds the value from the URL, shown in variable_tracker after step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of req.params.id at step 3?
Aundefined
B'user'
C'42'
Dnull
💡 Hint
Check the 'Controller State' column at step 3 in the execution_table.
At which step does the response get sent to the client?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Response Sent' column in the execution_table.
If the route was '/user/:id/profile', how would the controller receive the id parameter?
AIn req.params.profile
BIn req.params.id as usual
CIn req.query.id
DIt would not be available
💡 Hint
Route parameters are always available in req.params by their name, as shown in variable_tracker.
Concept Snapshot
Controller pattern in Express:
- Define controller functions to handle requests
- Use Express router to map routes to controllers
- Controllers receive req and res objects
- Controllers process data and send responses
- Keeps route logic clean and organized
Full Transcript
In Express, the controller pattern means separating the code that handles requests into functions called controllers. When a client sends a request, Express router matches the route and calls the appropriate controller function. The controller receives the request and response objects, processes the request (like reading URL parameters), and sends back a response. This keeps the code organized and easy to manage. For example, a route '/user/:id' uses a controller that reads req.params.id and sends it back in the response. The execution table shows each step from receiving the request to sending the response. Variables like req.params.id change from undefined to the actual value as the request is processed.