Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The Router() method creates a new router object to handle routes.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' will not add any prefix.
Using '/home' or '/user' does not match the required prefix.
✗ Incorrect
The app.use('/api', router) prefixes all routes in the router with '/api'.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The res.send() method sends a response body as a string.
4fill in blank
hardFill 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'
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.
✗ Incorrect
The get method defines a GET route, and json sends a JSON response.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Prefix routes with '/api', define a POST route, and send JSON response.