0
0
Expressframework~10 mins

Route matching order matters in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Route matching order matters
Start: Incoming HTTP Request
Check first route pattern
Run first route handler
Send Response
No
Check next route pattern
Run matched route handler
Send Response
No
No route matched
Send 404 Not Found
Express checks routes in the order they are defined. The first matching route runs its handler and stops further checks.
Execution Sample
Express
app.get('/user', handler1);
app.get('/user/:id', handler2);

// Request: GET /user
// Request: GET /user/123
Two routes defined: one for '/user' and one for '/user/:id'. Requests show which handler runs based on order.
Execution Table
StepRequest URLRoute CheckedMatch ResultHandler ExecutedResponse Sent
1/userGET /userMatchhandler1Response from handler1
2/user/123GET /userNo MatchNoneNo response yet
3/user/123GET /user/:idMatchhandler2Response from handler2
💡 Execution stops at first matching route handler; order affects which handler runs.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
Request URL/user or /user/123/user/user/123/user/123
Current RouteNoneGET /userGET /userGET /user/:id
Match FoundFalseTrueFalseTrue
Handler ExecutedNonehandler1Nonehandler2
Key Moments - 3 Insights
Why does the '/user' route handler run instead of '/user/:id' when requesting '/user'?
Because Express checks routes in order and stops at the first match. '/user' matches exactly, so '/user/:id' is never checked for '/user' (see execution_table step 1).
What happens if the order of routes is reversed?
If '/user/:id' is defined before '/user', then '/user' matches '/user/:id' with id='user', so the second route handler runs instead of the exact '/user' handler.
Why does Express stop checking routes after a match?
Express assumes the first matching route is the correct one to handle the request, so it runs that handler and sends a response, skipping further route checks (see exit_note).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which handler runs for the request '/user'?
Ahandler1
Bhandler2
CNo handler runs
DBoth handlers run
💡 Hint
Check step 1 in the execution_table where the request URL is '/user'.
At which step does the request '/user/123' find a matching route?
AStep 1
BStep 2
CStep 3
DNo match found
💡 Hint
Look at the 'Match Result' column for '/user/123' in execution_table.
If the route '/user/:id' was defined before '/user', what would happen for the request '/user'?
Ahandler1 runs
Bhandler2 runs with id='user'
CNo handler runs
DBoth handlers run
💡 Hint
Consider how Express matches routes in order and how '/user' fits the pattern '/user/:id'.
Concept Snapshot
Express routes are checked in the order they are defined.
The first route that matches the request URL runs its handler.
Later routes are ignored once a match is found.
Order matters especially when routes overlap like '/user' and '/user/:id'.
Define specific routes before general ones to avoid unexpected matches.
Full Transcript
In Express, when a request comes in, the server checks each route in the order they were added. It compares the request URL to each route pattern. If a route matches, Express runs that route's handler and sends the response immediately. It does not check any more routes after that. For example, if you have a route for '/user' and another for '/user/:id', and the request is for '/user', Express will match the first route and run its handler. If the request is '/user/123', it will skip the first route because it does not match exactly, then match the second route and run that handler. This means the order you define routes matters a lot. If you put the general route '/user/:id' before the specific '/user', then '/user' will match the general route with id='user', which might not be what you want. Always put more specific routes before general ones to control which handler runs.