Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new router instance in Express.
Express
const router = require('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.
Using 'app' is for the main app, not routers.
✗ Incorrect
The Router function creates a new router instance for grouping routes.
2fill in blank
mediumComplete the code to mount the router under the '/api' namespace.
Express
app.[1]('/api', router);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' or 'post' only handles those HTTP methods.
Using 'listen' is for starting the server.
✗ Incorrect
The use method mounts middleware or routers at a path prefix.
3fill in blank
hardFix the error in the router code to define a GET route at '/' inside the router.
Express
router.[1]('/', (req, res) => { res.send('Hello from API'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' handles POST requests, not GET.
Using 'use' is for middleware, not specific HTTP methods.
✗ Incorrect
The get method defines a handler for GET requests on the router.
4fill in blank
hardFill both blanks to create a router and export it for use in the main app.
Express
const [1] = require('express').Router(); module.exports = [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Exporting 'app' instead of 'router' causes errors.
Using 'express' as the export is incorrect.
✗ Incorrect
We create a router named router and export it with the same name.
5fill in blank
hardFill all three blanks to define a namespaced route that responds with JSON.
Express
const [1] = require('express').Router(); [1].[2]('/status', (req, res) => { res.[3]({ message: 'API is running' }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'json' sends plain text.
Using 'post' instead of 'get' changes the HTTP method.
✗ Incorrect
Create a router named router, define a GET route, and respond with JSON using json().