Route matching order matters because Express checks routes in the order you write them. The first route that matches the request is used, so order controls which code runs.
Route matching order matters in Express
app.METHOD(PATH, HANDLER) // Example: app.get('/users/all', handlerFunction) app.get('/users/:id', handlerFunction)
Routes are checked top to bottom in the order they appear in your code.
More specific routes should come before more general or wildcard routes.
app.get('/users/all', (req, res) => { res.send('All users') }) app.get('/users/:id', (req, res) => { res.send(`User ${req.params.id}`) })
app.get('/files/*', (req, res) => { res.send('Wildcard route') }) app.get('/files/special', (req, res) => { res.send('Special file') })
app.get('/files/special', (req, res) => { res.send('Special file') }) app.get('/files/*', (req, res) => { res.send('Wildcard route') })
This example shows the specific route before the parameter route. Requests to '/item/special' will match the specific route first and not the parameter route.
Placing the specific route first ensures correct matching.
import express from 'express' const app = express() app.get('/item/special', (req, res) => { res.send('Special Item') }) app.get('/item/:id', (req, res) => { res.send(`Item ID: ${req.params.id}`) }) app.listen(3000, () => { console.log('Server running on http://localhost:3000') })
Always put specific routes before parameter or wildcard routes to ensure correct matching.
Order affects which route handles the request, so plan route order carefully.
Use console logs or debugging to check which route runs if unsure.
Express matches routes in the order they are defined.
Specific routes should come before general or wildcard routes.
Route order controls which handler runs for a request.
Practice
app.get('/user', ...) and app.get('/:id', ...) are defined. Which route will handle /user if /:id is defined first?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/:idis defined before/user, the path/usermatches/:idfirst, so that handler runs.Final Answer:
The first matching route in order handles the request, so/:idwill handle/user. -> Option CQuick Check:
Route order controls matching = C [OK]
- Thinking Express runs all matching routes
- Assuming specific routes override order automatically
- Believing Express throws errors on overlapping routes
/about is handled before any dynamic /:page route in Express?Solution
Step 1: Recall route order importance
Specific routes like/aboutmust be defined before dynamic routes like/:pageto match first.Step 2: Check options for correct order
app.get('/about', handler); app.get('/:page', handler); defines/aboutfirst, then/:page, ensuring/aboutmatches before dynamic routes.Final Answer:
app.get('/about', handler); app.get('/:page', handler); -> Option BQuick Check:
Specific before dynamic = A [OK]
- Defining dynamic routes before specific ones
- Using app.use instead of app.get for specific routes
- Assuming order doesn't affect matching
app.get('/product/:id', (req, res) => res.send('Product ' + req.params.id));
app.get('/product/list', (req, res) => res.send('Product List'));What will be the response when a client requests
/product/list?Solution
Step 1: Check route order
The dynamic route/product/:idis defined before the specific/product/listroute.Step 2: Determine which route matches first
Request/product/listmatches/product/:idfirst, so it returns 'Product list'.Step 3: Check case sensitivity
The specific route sends 'Product List' with capital 'L', while the dynamic route sends 'Product ' + id, which would be 'Product list' if matched.Final Answer:
'Product List' -> Option AQuick Check:
Specific route matches only if defined before dynamic [OK]
- Assuming specific route matches first regardless of order
- Expecting 404 when dynamic route matches
- Thinking Express throws route conflict errors
app.get('/:category', handler1);
app.get('/books', handler2);Requests to
/books always go to handler1. How do you fix this?Solution
Step 1: Identify route order problem
The dynamic route/:categorymatches/booksfirst because it is defined before the specific route.Step 2: Fix by reordering routes
Moving the specific/booksroute before the dynamic one ensures/booksmatcheshandler2first.Final Answer:
Moveapp.get('/books', handler2)beforeapp.get('/:category', handler1). -> Option AQuick Check:
Specific routes before dynamic fix matching [OK]
- Changing route paths unnecessarily
- Using app.use instead of app.get incorrectly
- Ignoring route order importance
-
/api/users returns a user list-
/api/:resource returns data for any resourceHow should you order these routes to ensure
/api/users returns the user list and /api/products returns product data?Solution
Step 1: Understand route specificity and order
Specific routes like/api/usersmust be defined before dynamic routes like/api/:resourceto match correctly.Step 2: Apply correct order
Defining/api/usersfirst ensures requests to/api/usershit that route, while other resources match the dynamic route.Final Answer:
Define/api/usersfirst, then/api/:resource. -> Option DQuick Check:
Specific before dynamic for correct matching [OK]
- Defining dynamic route before specific API routes
- Using app.use incorrectly for route handlers
- Trying to merge routes instead of ordering
Ready to prove it?
Pass the test and this pattern moves unknown
