0
0
Expressframework~10 mins

Validating body fields in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Validating body fields
Request received
Extract body fields
Check each required field
Field missing or invalid?
YesSend error response
No
All fields valid?
YesProcess request
No
Send error response
When a request arrives, the server extracts body fields, checks if required fields exist and are valid, then either sends an error or processes the request.
Execution Sample
Express
app.post('/user', (req, res) => {
  const { name, age } = req.body;
  if (!name || typeof name !== 'string') {
    return res.status(400).send('Name is required and must be a string');
  }
  res.send('User created');
});
This code checks if the 'name' field exists and is a string before creating a user.
Execution Table
StepActionBody FieldsCondition CheckedResultResponse Sent
1Request received{ name: 'Alice', age: 30 }Check if 'name' exists and is stringTrueNo
2All fields valid{ name: 'Alice', age: 30 }No missing or invalid fieldsTrueNo
3Process request{ name: 'Alice', age: 30 }N/AUser createdYes: 200 OK, 'User created'
4Request received{ age: 30 }Check if 'name' exists and is stringFalseNo
5Send error response{ age: 30 }Missing or invalid 'name'FalseYes: 400 Bad Request, 'Name is required and must be a string'
💡 Execution stops after sending response either success or error.
Variable Tracker
VariableStartAfter Step 1After Step 4Final
req.body{}{ name: 'Alice', age: 30 }{ age: 30 }N/A
nameundefined'Alice'undefinedN/A
ageundefined3030N/A
Key Moments - 2 Insights
Why does the server send an error if 'name' is missing or not a string?
Because the condition in step 1 or 4 fails, triggering the error response in step 5 as shown in the execution_table.
What happens if all required fields are valid?
The code proceeds to process the request and sends a success response as shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response when 'name' is missing in the body?
A500 Internal Server Error
B200 OK with message 'User created'
C400 Bad Request with message 'Name is required and must be a string'
DNo response sent
💡 Hint
Check rows 4 and 5 in the execution_table where 'name' is missing.
At which step does the server confirm all fields are valid?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'All fields valid' action in the execution_table.
If the 'name' field was a number instead of a string, what would happen?
AThe server sends a 400 error response
BThe server sends a success response
CThe server ignores the 'name' field
DThe server crashes
💡 Hint
The condition checks type of 'name' as string in step 1 and 4.
Concept Snapshot
Validating body fields in Express:
- Extract fields from req.body
- Check required fields exist and types match
- If invalid, send 400 error response
- If valid, proceed with request
- Always respond to avoid hanging requests
Full Transcript
When Express receives a POST request, it extracts the body fields from req.body. The code checks if required fields like 'name' exist and are the correct type, for example a string. If the check fails, the server sends a 400 Bad Request response with an error message. If all fields are valid, the server processes the request and sends a success response. This flow ensures the server only accepts valid data and informs the client when data is missing or wrong.