0
0
Expressframework~20 mins

Why routing matters in Express - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Routing Mastery in Express
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a route matches in Express?
Consider this Express code snippet:
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!'); });
AThe server sends back an empty response
BThe server responds with a 404 Not Found error
CThe server responds with the text 'Hello World!'
DThe server crashes with an error
Attempts:
2 left
💡 Hint
Think about what the res.send method does in Express.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this route definition
Look at this Express route:
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); });
AMissing comma between path and callback function
BUsing arrow function instead of regular function
CIncorrect use of req.params.id
DMissing semicolon after res.send
Attempts:
2 left
💡 Hint
Check the parameters passed to app.get carefully.
state_output
advanced
2:00remaining
What is the output when multiple routes match?
Given this Express code:
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'); });
A'Special Item'
B'Item special'
C404 Not Found
DServer error
Attempts:
2 left
💡 Hint
Express matches routes in the order they are defined.
🔧 Debug
advanced
2:00remaining
What happens when a route handler calls next()?
Look at this Express code:
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'); });
AThe server crashes with an error
BThe server hangs and never responds
CThe server responds with 404 Not Found
DThe server responds with 'Data loaded'
Attempts:
2 left
💡 Hint
Think about what calling next() does in a route handler.
🧠 Conceptual
expert
2:00remaining
Why is route order important in Express?
Imagine you have these routes:
app.get('/user/:id', handler1);
app.get('/user/profile', handler2);

What is the effect of defining /user/:id before /user/profile?
ARequests to '/user/profile' will be handled by handler1, not handler2
BRequests to '/user/profile' will be handled by handler2 as expected
CRequests to '/user/profile' will cause the server to crash
DExpress will throw an error about duplicate routes
Attempts:
2 left
💡 Hint
Express matches routes in the order they are defined and stops at the first match.