Challenge - 5 Problems
Routing Mastery in Express
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a route matches in Express?
Consider this Express code snippet:
What will the server send back when a client requests
app.get('/hello', (req, res) => { res.send('Hello World!'); });What will the server send back when a client requests
/hello?Express
app.get('/hello', (req, res) => { res.send('Hello World!'); });
Attempts:
2 left
💡 Hint
Think about what the
res.send method does in Express.✗ Incorrect
When the route matches '/hello', the callback runs and sends 'Hello World!' as the response body.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this route definition
Look at this Express route:
What is wrong with this code?
app.get('/user/:id' (req, res) => { res.send(req.params.id); });What is wrong with this code?
Express
app.get('/user/:id' (req, res) => { res.send(req.params.id); });Attempts:
2 left
💡 Hint
Check the parameters passed to app.get carefully.
✗ Incorrect
The route path and the callback function must be separated by a comma.
❓ state_output
advanced2:00remaining
What is the output when multiple routes match?
Given this Express code:
What will the server respond to a request for
app.get('/item/:id', (req, res) => { res.send('Item ' + req.params.id); });
app.get('/item/special', (req, res) => { res.send('Special Item'); });What will the server respond to a request for
/item/special?Express
app.get('/item/:id', (req, res) => { res.send('Item ' + req.params.id); }); app.get('/item/special', (req, res) => { res.send('Special Item'); });
Attempts:
2 left
💡 Hint
Express matches routes in the order they are defined.
✗ Incorrect
The first route matches '/item/:id' and captures 'special' as id, so it responds with 'Item special'. The second route is never reached.
🔧 Debug
advanced2:00remaining
What happens when a route handler calls next()?
Look at this Express code:
When a client requests
app.get('/api/data', (req, res, next) => { next(); });
app.get('/api/data', (req, res) => { res.send('Data loaded'); });When a client requests
/api/data, what happens?Express
app.get('/api/data', (req, res, next) => { next(); }); app.get('/api/data', (req, res) => { res.send('Data loaded'); });
Attempts:
2 left
💡 Hint
Think about what calling next() does in a route handler.
✗ Incorrect
Calling next() in the first handler passes control to the next matching route handler (the second one), which sends 'Data loaded'. Express chains multiple handlers for the same path and method.
🧠 Conceptual
expert2:00remaining
Why is route order important in Express?
Imagine you have these routes:
What is the effect of defining
app.get('/user/:id', handler1);
app.get('/user/profile', handler2);What is the effect of defining
/user/:id before /user/profile?Attempts:
2 left
💡 Hint
Express matches routes in the order they are defined and stops at the first match.
✗ Incorrect
Since '/user/:id' matches any path starting with '/user/', including '/user/profile', it will catch that request first if defined before '/user/profile'. So handler2 never runs.