Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Express module.
Express
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express'
Forgetting to put the module name in quotes
✗ Incorrect
The Express module is imported using require('express').
2fill in blank
mediumComplete the code to parse JSON request bodies in Express.
Express
app.use([1].json()); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using bodyParser without importing it
Using app.json() instead of express.json()
✗ Incorrect
Express has built-in middleware to parse JSON bodies: express.json().
3fill in blank
hardFix the error in the POST route handler to access the request body.
Express
app.post('/data', (req, res) => { const data = req.[1]; res.send(data); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.params which is for URL parameters
Using req.query which is for URL query strings
✗ Incorrect
The POST request body is accessed via req.body in Express.
4fill in blank
hardFill both blanks to create a test using supertest to POST JSON data.
Express
request(app) .post('/submit') .send({ name: 'Alice' }) .expect([1], [2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 instead of 200 for success
Using 'text' instead of 'json' for JSON responses
✗ Incorrect
The test expects a 200 status code and a JSON response.
5fill in blank
hardFill all three blanks to complete the test that sends JSON and checks the response.
Express
test('POST /api/data', async () => { const response = await request(app) .post('/api/data') .set('[1]', 'application/json') .send({ key: 'value' }); expect(response.status).toBe([2]); expect(response.body).toHaveProperty('[3]'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Authorization' header instead of 'Content-Type'
Expecting status 404 instead of 200
Checking for a wrong property name in response body
✗ Incorrect
The test sets the 'Content-Type' header to 'application/json', expects status 200, and checks the response body has the 'key' property.