0
0
Expressframework~20 mins

Route matching order matters in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Route Mastery in Express
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Which route handler responds to GET /users/123?
Given these Express routes, which handler will respond when a client requests GET /users/123?
Express
const express = require('express');
const app = express();

app.get('/users/profile', (req, res) => {
  res.send('User profile route');
});

app.get('/users/:id', (req, res) => {
  res.send('User ID route');
});
ABoth handlers respond, sending both messages.
BNo handler matches, so a 404 error is sent.
CThe handler for '/users/:id' responds with 'User ID route'.
DThe handler for '/users/profile' responds with 'User profile route'.
Attempts:
2 left
💡 Hint
Express matches routes in the order they are defined and picks the first matching route.
📝 Syntax
intermediate
2:00remaining
What error occurs with this route order?
Consider these Express routes. What error or behavior occurs when requesting GET /about?
Express
const express = require('express');
const app = express();

app.get('/about', (req, res) => {
  res.send('About page');
});

app.get('/:page', (req, res) => {
  res.send('Page: ' + req.params.page);
});
AThe '/about' route responds with 'About page'.
BThe '/:page' route responds with 'Page: about'.
CA syntax error occurs due to route conflict.
DExpress throws a runtime error about ambiguous routes.
Attempts:
2 left
💡 Hint
Express matches routes in the order they are defined.
component_behavior
advanced
2:00remaining
What is the output when route order is reversed?
If the routes below are reversed in order, what response will GET /contact return?
Express
const express = require('express');
const app = express();

app.get('/:page', (req, res) => {
  res.send('Page: ' + req.params.page);
});

app.get('/contact', (req, res) => {
  res.send('Contact page');
});
AResponds with 'Page: contact'.
BResponds with 'Contact page'.
CResponds with 404 Not Found.
DThrows a runtime error due to duplicate routes.
Attempts:
2 left
💡 Hint
Express picks the first matching route it finds.
🔧 Debug
advanced
2:00remaining
Why does this Express app always respond with 'Catch-all'?
Examine the code. Why does every GET request respond with 'Catch-all', even for '/home' or '/about'?
Express
const express = require('express');
const app = express();

app.get('*', (req, res) => {
  res.send('Catch-all');
});

app.get('/home', (req, res) => {
  res.send('Home page');
});

app.get('/about', (req, res) => {
  res.send('About page');
});
AThe '/home' and '/about' routes override the catch-all route.
BExpress throws an error because '*' is an invalid route pattern.
CThe catch-all route is defined first and matches all requests, blocking later routes.
DThe app crashes due to duplicate route definitions.
Attempts:
2 left
💡 Hint
Route order affects which handler runs first.
🧠 Conceptual
expert
3:00remaining
How to ensure specific routes take priority over dynamic routes?
You have these routes in Express: 1. app.get('/:category', ...) 2. app.get('/products', ...) How should you order them to ensure '/products' route is matched first when requested?
AUse app.use() instead of app.get() for '/products'.
BDefine app.get('/products', ...) before app.get('/:category', ...).
CDefine app.get('/:category', ...) before app.get('/products', ...).
DUse regular expressions in the route paths to differentiate.
Attempts:
2 left
💡 Hint
Express matches routes in the order they are defined.