📖 Scenario: You are building a simple Express server that responds to different URL paths. The order in which you define routes affects which response the server sends.
🎯 Goal: Learn how to define routes in Express so that the server matches the correct route based on the order they are written.
📋 What You'll Learn
Create an Express app with three routes: a specific route, a parameter route, and a catch-all route
Define a variable to hold the port number
Use the correct order of route definitions so the server responds properly
Add the final line to start the server listening on the port
💡 Why This Matters
🌍 Real World
Web servers often have many routes. Defining them in the right order ensures users get the correct pages.
💼 Career
Backend developers must understand route matching to build reliable APIs and web servers.
Progress0 / 4 steps
1
Set up Express app and routes
Create an Express app by requiring express and calling it to get app. Then define three routes in this order: /about that sends "About page", /user/:id that sends "User ID: " plus the id parameter, and a catch-all * route that sends "Page not found".
Express
Hint
Remember to require Express and create the app first. Then add the three routes in the order given.
2
Add port configuration
Create a constant called port and set it to 3000.
Express
Hint
Just add a constant named port and assign it the number 3000.
3
Reorder routes to show matching order importance
Move the catch-all app.get('*', ...) route to be the first route defined, before /about and /user/:id. Keep the rest of the code unchanged.
Express
Hint
Cut the catch-all route and paste it before the other two routes.
4
Start the server listening
Add app.listen(port) with a callback that sends console message Server running on port 3000.
Express
Hint
Use app.listen with port and a callback that logs the message.
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