Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a POST route for '/submit'.
Express
app.[1]('/submit', (req, res) => { res.send('Form submitted'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'post' for form submission routes.
Using 'put' or 'delete' which are for other HTTP actions.
✗ Incorrect
The post method defines a POST route in Express.
2fill in blank
mediumComplete the code to access JSON data sent in the POST request body.
Express
app.post('/data', (req, res) => { const userData = req.[1]; res.json(userData); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'params' which is for URL parameters.
Using 'query' which is for URL query strings.
✗ Incorrect
The body property contains data sent in the POST request body.
3fill in blank
hardFix the error in the code to parse JSON body data correctly.
Express
const express = require('express'); const app = express(); app.[1](express.json()); app.post('/info', (req, res) => { res.send(req.body); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'use' for middleware.
Not adding JSON parsing middleware at all.
✗ Incorrect
The use method adds middleware to parse JSON bodies before routes handle requests.
4fill in blank
hardFill both blanks to send a JSON response with a status code 201.
Express
app.post('/create', (req, res) => { res.[1](201).[2]({ message: 'Created' }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'json' for JSON responses.
Not setting the status code properly.
✗ Incorrect
Use status(201) to set the HTTP status code and json() to send a JSON response.
5fill in blank
hardFill all three blanks to create a POST route that reads JSON body, logs it, and responds with a success message.
Express
app.[1]('/log', (req, res) => { const data = req.[2]; console.log(data); res.[3]({ status: 'success' }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'post' for the route.
Trying to access data from 'params' or 'query' instead of 'body'.
Using 'send' instead of 'json' for the response.
✗ Incorrect
Define a POST route with post, access the JSON body with body, and respond with JSON using json.