0
0
Expressframework~10 mins

Route prefixing in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a router instance.

Express
const express = require('express');
const router = express.[1]();
Drag options to blanks, or click blank then click option'
ARouter
Broute
Capp
Dlisten
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'route' instead of 'Router' causes an error because 'route' is not a function.
Using 'app' is incorrect because it refers to the main app, not a router.
2fill in blank
medium

Complete the code to prefix all routes in the router with '/api'.

Express
app.use([1], router);
Drag options to blanks, or click blank then click option'
A'/home'
B'/'
C'/user'
D'/api'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' will not add any prefix.
Using '/home' or '/user' does not match the required prefix.
3fill in blank
hard

Fix the error in the route definition to respond with 'Hello World'.

Express
router.get('/hello', (req, res) => {
  res.[1]('Hello World');
});
Drag options to blanks, or click blank then click option'
Ajson
Bwrite
Csend
Drender
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'write' causes the response to not end properly.
Using 'render' expects a template, which is not used here.
4fill in blank
hard

Fill both blanks to create a router that handles GET requests at '/users' and responds with JSON.

Express
router.[1]('/users', (req, res) => {
  res.[2]({ message: 'User list' });
});
Drag options to blanks, or click blank then click option'
Aget
Bsend
Cjson
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get' changes the route method.
Using 'send' instead of 'json' sends plain text, not JSON.
5fill in blank
hard

Fill all three blanks to prefix routes with '/api', create a POST route at '/login', and send a JSON response.

Express
app.use([1], router);

router.[2]('/login', (req, res) => {
  res.[3]({ success: true });
});
Drag options to blanks, or click blank then click option'
A'/api'
Bpost
Cjson
D'/user'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/user' as prefix instead of '/api'.
Using 'get' instead of 'post' for the login route.
Using 'send' instead of 'json' for JSON response.