What happens if you place a general route before a specific route in Express?
The general route will match first and handle the request, so the specific route will never be reached.
Click to reveal answer
beginner
Why does route order matter in Express?
Express checks routes in the order they are defined and stops at the first match. So, order controls which route handles a request.
Click to reveal answer
intermediate
Given these routes, which one matches the URL '/user/profile'? app.get('/user/profile', ...) app.get('/user/:id', ...)
If /user/:id is defined before /user/profile, it will match '/user/profile' as id='profile'. So order matters to get the right route.
Click to reveal answer
beginner
How can you ensure a specific route is matched before a general one?
Define the specific route first in your code, then the general route after it.
Click to reveal answer
intermediate
What is the effect of placing a wildcard route like app.get('*', ...) at the top?
It will catch all requests and prevent any other routes from running, blocking specific routes below it.
Click to reveal answer
In Express, which route should be defined first to correctly handle '/user/profile'?
Aapp.get('/user', ...)
Bapp.get('/user/:id', ...)
Capp.get('*', ...)
Dapp.get('/user/profile', ...)
✗ Incorrect
Specific routes like '/user/profile' should be defined before parameter routes like '/user/:id' to match correctly.
What happens if you put a wildcard route app.get('*', ...) before other routes?
AIt will match only GET requests to '/'
BOther routes will never be reached.
CIt will be ignored.
DIt will only match unknown routes.
✗ Incorrect
Wildcard routes catch all requests and block routes defined after them.
Express matches routes in which order?
AAlphabetical order
BBy route length
COrder routes are defined in code
DRandom order
✗ Incorrect
Express checks routes in the order they appear in the code.
If you want a route to catch all unmatched requests, where should you place it?
AAt the bottom of the route list
BIn the middle of the route list
CAt the top of the route list
DAnywhere, order does not matter
✗ Incorrect
Catch-all routes should be last to avoid blocking other routes.
Which route will match '/user/123' if defined in this order? 1. app.get('/user/:id', ...) 2. app.get('/user/profile', ...)
A/user/:id
B/user/profile
CBoth routes
DNo route
✗ Incorrect
The first matching route '/user/:id' will handle '/user/123'.
Explain why the order of route definitions matters in Express and give an example.
Think about how Express stops checking after the first match.
You got /3 concepts.
Describe what happens if a wildcard route is placed before other routes in Express.
Consider the effect of a catch-all route on routing.
You got /3 concepts.
Practice
(1/5)
1. In Express, why does the order of route definitions matter? app.get('/user', ...) and app.get('/:id', ...) are defined. Which route will handle /user if /:id is defined first?
easy
A. The most specific route always handles the request regardless of order.
B. Express matches all routes and runs all handlers regardless of order.
C. The first matching route in order handles the request, so /:id will handle /user.
D. Express throws an error if routes overlap.
Solution
Step 1: Understand Express route matching order
Express checks routes in the order they are defined and stops at the first match.
Step 2: Analyze the given routes
If /:id is defined before /user, the path /user matches /:id first, so that handler runs.
Final Answer:
The first matching route in order handles the request, so /:id will handle /user. -> Option C
Quick Check:
Route order controls matching = C [OK]
Hint: Remember: Express uses first match wins rule [OK]
Common Mistakes:
Thinking Express runs all matching routes
Assuming specific routes override order automatically
Believing Express throws errors on overlapping routes
2. Which of the following is the correct way to define routes so that /about is handled before any dynamic /:page route in Express?
easy
A. app.get('/:page', handler); app.get('/about', handler);
B. app.get('/about', handler); app.get('/:page', handler);
C. app.get('/about', handler); app.use('/:page', handler);
D. app.use('/:page', handler); app.get('/about', handler);
Solution
Step 1: Recall route order importance
Specific routes like /about must be defined before dynamic routes like /:page to match first.
Step 2: Check options for correct order
app.get('/about', handler); app.get('/:page', handler); defines /about first, then /:page, ensuring /about matches before dynamic routes.
Final Answer:
app.get('/about', handler); app.get('/:page', handler); -> Option B
Quick Check:
Specific before dynamic = A [OK]
Hint: Put specific routes before dynamic ones [OK]
Common Mistakes:
Defining dynamic routes before specific ones
Using app.use instead of app.get for specific routes