Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a GET route for the path '/hello'.
Express
app.[1]('/hello', (req, res) => { res.send('Hello World!'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get' for a GET route.
Using 'put' or 'delete' which are for other HTTP methods.
✗ Incorrect
The get method defines a route that responds to GET requests.
2fill in blank
mediumComplete the code to send a JSON response with a message in the GET route.
Express
app.get('/json', (req, res) => { res.[1]({ message: 'Hello JSON' }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
sendFile which sends a file, not JSON.Using
render which is for templates.✗ Incorrect
The json method sends a JSON response to the client.
3fill in blank
hardFix the error in the GET route that tries to access a URL parameter.
Express
app.get('/user/:id', (req, res) => { const userId = req.params.[1]; res.send(`User ID is ${userId}`); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different name than the URL parameter.
Trying to access
req.param instead of req.params.✗ Incorrect
The URL parameter is named 'id', so access it with req.params.id.
4fill in blank
hardFill both blanks to create a GET route that sends a status code and a JSON message.
Express
app.[1]('/status', (req, res) => { res.status([2]).json({ message: 'All good' }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST method instead of GET.
Using 404 status code which means not found.
✗ Incorrect
Use get for GET route and 200 for successful status code.
5fill in blank
hardFill all three blanks to create a GET route that reads a query parameter and sends it back in JSON.
Express
app.[1]('/search', (req, res) => { const term = req.query.[2]; res.[3]({ searchTerm: term }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
params instead of query for query parameters.Using
send instead of json for JSON response.✗ Incorrect
The route uses get, reads query parameter query, and sends JSON response.