0
0
Expressframework~10 mins

JSON request and response patterns 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 parse JSON data from a POST request in Express.

Express
app.use(express.[1]());
Drag options to blanks, or click blank then click option'
Ajson
Burlencoded
Cstatic
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using express.urlencoded() instead of express.json() for JSON data
Forgetting to use any body parser middleware
2fill in blank
medium

Complete the code to send a JSON response with a message.

Express
res.[1]({ message: 'Hello, world!' });
Drag options to blanks, or click blank then click option'
Aredirect
BsendFile
Crender
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.send() without JSON object
Using res.render() which is for templates
3fill in blank
hard

Fix the error in the code to correctly access JSON data sent in the request body.

Express
const name = req.body.[1];
Drag options to blanks, or click blank then click option'
Aparams
Bquery
Cname
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to get data from req.params or req.query instead of req.body
Accessing a wrong property name
4fill in blank
hard

Fill both blanks to create a POST route that receives JSON and sends a JSON response.

Express
app.[1]('/submit', (req, res) => {
  const data = req.body;
  res.[2]({ received: data });
});
Drag options to blanks, or click blank then click option'
Apost
Bget
Cjson
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using app.get instead of app.post for POST routes
Using res.send instead of res.json for JSON responses
5fill in blank
hard

Fill all three blanks to parse JSON, handle a POST request, and send a JSON response with a status code.

Express
app.use(express.[1]());

app.[2]('/api/data', (req, res) => {
  const info = req.body;
  res.status([3]).json({ success: true, data: info });
});
Drag options to blanks, or click blank then click option'
Ajson
Bpost
C200
Durlencoded
Attempts:
3 left
💡 Hint
Common Mistakes
Using express.urlencoded() instead of express.json() for JSON parsing
Using app.get instead of app.post for POST routes
Not setting status code or using wrong status code