0
0
Expressframework~10 mins

Request and response schemas in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Request and response schemas
Client sends request
Server receives request
Validate request schema
Yes / No
Send error
Prepare response data
Validate response schema
Yes / No
Send error
Client receives response
This flow shows how a server checks the request data matches a schema before processing, then validates the response data before sending it back.
Execution Sample
Express
const schema = {
  body: { name: 'string', age: 'number' },
};

const validate = (data, sch) => {
  for (const [key, type] of Object.entries(sch)) {
    if (typeof data[key] !== type) {
      return false;
    }
  }
  return true;
};

app.post('/user', (req, res) => {
  const validationResult = validate(req.body, schema.body);
  if (!validationResult) {
    return res.status(400).send('Invalid data');
  }
  const response = { message: `Hello, ${req.body.name}` };
  res.send(response);
});
This code checks the request body matches the schema before responding with a greeting message.
Execution Table
StepActionRequest BodyValidation ResultResponse Sent
1Receive request{ name: 'Alice', age: 30 }Not checked yetNone
2Validate request body{ name: 'Alice', age: 30 }ValidNone
3Process request{ name: 'Alice', age: 30 }ValidNone
4Prepare response{ name: 'Alice', age: 30 }Valid{ message: 'Hello, Alice' }
5Send response{ name: 'Alice', age: 30 }Valid{ message: 'Hello, Alice' }
6Request complete{ name: 'Alice', age: 30 }ValidResponse sent
💡 Request ends after sending valid response or error if validation fails
Variable Tracker
VariableStartAfter Step 2After Step 4Final
req.bodyundefined{ name: 'Alice', age: 30 }{ name: 'Alice', age: 30 }{ name: 'Alice', age: 30 }
validationResultundefinedtruetruetrue
responseundefinedundefined{ message: 'Hello, Alice' }{ message: 'Hello, Alice' }
Key Moments - 3 Insights
Why do we validate the request body before processing?
Validating early (see step 2 in execution_table) prevents processing bad or unexpected data, avoiding errors later.
What happens if the request body does not match the schema?
The server sends an error response immediately (not shown in this trace), stopping further processing.
Why validate the response before sending it?
Validating response ensures the server sends data in the expected format, keeping client and server in sync.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the validation result at step 2?
AInvalid
BValid
CNot checked yet
DError
💡 Hint
Check the 'Validation Result' column at step 2 in execution_table
At which step is the response prepared?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for when response is created
If the request body was missing the 'age' field, what would happen at step 2?
AValidation would fail and send error
BResponse would be sent anyway
CValidation would pass
DServer would crash
💡 Hint
Refer to key_moments about request validation and error sending
Concept Snapshot
Request and response schemas in Express:
- Validate incoming request data matches schema before processing
- If invalid, send error response immediately
- Process valid requests and prepare response data
- Validate response data matches schema before sending
- Helps keep client-server data consistent and safe
Full Transcript
In Express, when a client sends a request, the server first checks if the request data matches a defined schema. This validation step ensures the data is correct and safe to use. If the data is invalid, the server sends an error response and stops processing. If valid, the server processes the request, prepares the response data, and then validates the response against a schema before sending it back. This double validation keeps communication clear and prevents errors. The execution table shows each step: receiving the request, validating, processing, preparing response, and sending it. Variables like request body, validation result, and response data change as the server works through these steps.