0
0
Expressframework~10 mins

Route parameters in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Route parameters
Client sends request to URL with parameter
Express matches route pattern with :param
Extract parameter value from URL
Pass parameter value to route handler
Route handler uses parameter to respond
Send response back to client
Express checks the URL for parts marked as parameters, extracts their values, and passes them to the handler to create a response.
Execution Sample
Express
const express = require('express');
const app = express();
app.get('/user/:id', (req, res) => {
  res.send(`User ID is ${req.params.id}`);
});
This code sets up a route that captures the user ID from the URL and sends it back in the response.
Execution Table
StepRequest URLRoute Pattern MatchedExtracted paramHandler ActionResponse Sent
1/user/42/user/:idid = '42'Send 'User ID is 42''User ID is 42'
2/user/abc/user/:idid = 'abc'Send 'User ID is abc''User ID is abc'
3/user/No matchN/ANo handler called404 Not Found
💡 Execution stops when no route matches or response is sent.
Variable Tracker
VariableStartAfter Request 1After Request 2After Request 3
req.params.idundefined'42''abc'undefined
Key Moments - 2 Insights
Why does the route not match when the URL is '/user/'?
Because the route pattern '/user/:id' expects a value after '/user/', so '/user/' without a parameter does not match (see execution_table row 3).
How does Express know which part of the URL is the parameter?
Express uses the colon ':' in the route pattern to mark a parameter, so it extracts the corresponding part from the URL (see execution_table rows 1 and 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is req.params.id when the request URL is '/user/abc'?
Aundefined
B'42'
C'abc'
Dnull
💡 Hint
Check the 'Extracted param' column for step 2 in the execution_table.
At which step does the route pattern fail to match the request URL?
AStep 2
BStep 3
CStep 1
DNone
💡 Hint
Look for 'No match' in the 'Route Pattern Matched' column in the execution_table.
If the route was changed to '/user/:id/profile', what would happen to the request URL '/user/42'?
AIt does not match because '/profile' is missing
BIt matches and extracts id = '42'
CIt matches but id is undefined
DIt matches and extracts id = 'profile'
💡 Hint
Think about the exact pattern matching shown in the execution_table and how extra path parts affect matching.
Concept Snapshot
Express route parameters use ':' to mark parts of the URL as variables.
When a request matches, Express extracts these values into req.params.
Handlers access parameters via req.params.paramName.
If the URL part is missing, the route does not match.
This allows dynamic URLs like '/user/123' to be handled easily.
Full Transcript
In Express, route parameters let you capture parts of the URL as variables. When a client sends a request like '/user/42', Express matches it to a route pattern like '/user/:id'. The ':id' means Express will take the '42' from the URL and put it in req.params.id. The route handler can then use this value to create a response, such as sending back 'User ID is 42'. If the URL does not have the parameter part, like '/user/', the route won't match and Express will not call the handler. This way, you can create flexible routes that respond differently based on URL parts.