0
0
Expressframework~10 mins

JSON request and response patterns in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - JSON request and response patterns
Client sends JSON request
Express server receives request
Parse JSON body middleware
Route handler processes data
Create JSON response object
Send JSON response to client
Client receives JSON
This flow shows how an Express server receives a JSON request, processes it, and sends back a JSON response.
Execution Sample
Express
app.use(express.json());

app.post('/data', (req, res) => {
  const name = req.body.name;
  res.json({ greeting: `Hello, ${name}!` });
});
This code receives a JSON request with a name, then responds with a JSON greeting.
Execution Table
StepActionInput/StateOutput/State Change
1Client sends POST /data with JSON body {"name":"Alice"}Request body: {"name":"Alice"}Request received by server
2express.json() middleware parses JSONRaw request bodyreq.body = { name: "Alice" }
3Route handler reads req.body.namereq.body = { name: "Alice" }name = "Alice"
4Route handler creates response JSONname = "Alice"Response body = { greeting: "Hello, Alice!" }
5Server sends JSON responseResponse bodyClient receives JSON { greeting: "Hello, Alice!" }
6Execution endsResponse sentConnection handled successfully
💡 Request handled and response sent, no further action
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
req.bodyundefined{"name":"Alice"}{"name":"Alice"}{"name":"Alice"}{"name":"Alice"}
nameundefinedundefined"Alice""Alice""Alice"
response JSONundefinedundefinedundefined{"greeting":"Hello, Alice!"}{"greeting":"Hello, Alice!"}
Key Moments - 2 Insights
Why do we need express.json() middleware before accessing req.body?
Because without express.json(), req.body is undefined. The middleware parses the raw JSON string into a JavaScript object, as shown in step 2 of the execution_table.
What happens if the client sends invalid JSON?
express.json() middleware will return an error and the route handler won't run. This prevents the server from crashing and ensures only valid JSON is processed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of 'name'?
Aundefined
B"Alice"
Cnull
D"name"
💡 Hint
Check the 'Input/State' and 'Output/State Change' columns at step 3 in execution_table
At which step does req.body get its parsed JSON object?
AStep 2
BStep 4
CStep 1
DStep 5
💡 Hint
Look for when express.json() middleware runs in execution_table
If the client sends {"name":"Bob"} instead, what changes in variable_tracker?
AOnly name changes to Bob
BNo changes, still Alice
Creq.body and name change to Bob
Dresponse JSON becomes empty
💡 Hint
See how req.body and name are assigned in variable_tracker after Step 3
Concept Snapshot
Express JSON request and response pattern:
- Use app.use(express.json()) to parse JSON body
- Access data via req.body in route handlers
- Send JSON response with res.json({key: value})
- Handles JSON automatically both ways
- Ensures client-server data exchange in JSON format
Full Transcript
This visual execution shows how an Express server handles JSON requests and responses. First, the client sends a POST request with a JSON body containing a name. The express.json() middleware parses this raw JSON string into a JavaScript object and assigns it to req.body. The route handler then reads the name property from req.body and creates a greeting message. Finally, the server sends this greeting back as a JSON response using res.json(). The variable tracker shows how req.body and name change during execution. Key moments include the necessity of express.json() middleware to parse JSON and handling invalid JSON gracefully. The quiz tests understanding of variable values at each step and how changes in input affect the flow.