0
0
Expressframework~5 mins

JSON request and response patterns in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of using JSON in Express request and response?
JSON is used to send and receive data in a simple, text-based format that both client and server can easily understand and parse.
Click to reveal answer
beginner
How do you parse JSON data sent in a POST request in Express?
Use the built-in middleware express.json() to automatically parse incoming JSON request bodies and make the data available in req.body.
Click to reveal answer
beginner
Which method sends a JSON response in Express?
The res.json() method sends a JSON response. It converts a JavaScript object or array into JSON and sets the Content-Type header to application/json.
Click to reveal answer
intermediate
Why should you set Content-Type to application/json in Express responses?
Setting Content-Type to application/json tells the client that the response body contains JSON data, so the client can parse it correctly.
Click to reveal answer
intermediate
What happens if you forget to use express.json() middleware when sending JSON in a POST request?
Without express.json(), Express won't parse the JSON body, so req.body will be undefined or empty, making it impossible to access the sent data.
Click to reveal answer
Which Express middleware is used to parse JSON request bodies?
Aexpress.json()
Bexpress.urlencoded()
Cexpress.static()
Dexpress.text()
How do you send a JSON response in Express?
Ares.sendJson()
Bres.send()
Cres.sendFile()
Dres.json()
What header does Express set automatically when using res.json()?
AContent-Type: text/plain
BContent-Type: text/html
CContent-Type: application/json
DContent-Type: application/xml
If you forget to use express.json() middleware, what will req.body be for JSON POST requests?
AAn empty object {}
BUndefined or empty
CA string with raw JSON
DAn error is thrown
Which method would you use to parse URL-encoded form data in Express?
Aexpress.urlencoded()
Bexpress.json()
Cexpress.raw()
Dexpress.text()
Explain how to handle JSON data in an Express POST request and send a JSON response.
Think about middleware and response methods for JSON.
You got /4 concepts.
    Describe what happens if you send JSON data to an Express server without using express.json() middleware.
    Focus on request body parsing.
    You got /4 concepts.