0
0
Expressframework~10 mins

Testing POST with request body in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Express module.

Express
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Aexpress
Bhttp
Cfs
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express'
Forgetting to put the module name in quotes
2fill in blank
medium

Complete the code to parse JSON request bodies in Express.

Express
app.use([1].json());
Drag options to blanks, or click blank then click option'
AbodyParser
Bexpress
Crouter
Dhttp
Attempts:
3 left
💡 Hint
Common Mistakes
Using bodyParser without importing it
Using app.json() instead of express.json()
3fill in blank
hard

Fix 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'
Aparams
Bheaders
Cquery
Dbody
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.params which is for URL parameters
Using req.query which is for URL query strings
4fill in blank
hard

Fill 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'
A200
Bjson
Ctext
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 instead of 200 for success
Using 'text' instead of 'json' for JSON responses
5fill in blank
hard

Fill 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'
AContent-Type
B200
Ckey
DAuthorization
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