0
0
Expressframework~10 mins

Request and response schemas 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 library.

Express
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Afs
Bhttp
Cpath
Dexpress
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express' for the import.
Forgetting to put quotes around the package name.
2fill in blank
medium

Complete the code to define a POST route that expects JSON data.

Express
app.post('/data', (req, res) => {
  const userData = req.[1];
  res.send('Received');
});
Drag options to blanks, or click blank then click option'
Aquery
Bparams
Cbody
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.params or req.query to get JSON data.
Not using middleware to parse JSON before accessing req.body.
3fill in blank
hard

Fix the error in the response to send JSON data correctly.

Express
res.[1]({ message: 'Success' });
Drag options to blanks, or click blank then click option'
Ajson
Bend
Cwrite
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.send() without setting headers explicitly.
Using res.write() or res.end() which are lower-level methods.
4fill in blank
hard

Fill both blanks to validate request data and send an error response if invalid.

Express
if (!req.body.name || typeof req.body.name !== '[1]') {
  return res.status([2]).json({ error: 'Name is required and must be a string' });
}
Drag options to blanks, or click blank then click option'
Astring
Bnumber
C400
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong data type in typeof check.
Using 404 status code which means not found.
5fill in blank
hard

Fill all three blanks to create a response schema that sends user info with id, name, and active status.

Express
res.json({
  id: req.body.[1],
  name: req.body.[2],
  active: req.body.[3]
});
Drag options to blanks, or click blank then click option'
AuserId
Bname
Cactive
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property names that don't exist in req.body.
Including unrelated fields like email when not required.