0
0
Expressframework~20 mins

Request and response schemas in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Schema Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Express route when sending a valid JSON body?

Consider this Express route that expects a JSON body with a name property and responds with a greeting.

app.post('/greet', (req, res) => {
  const { name } = req.body;
  if (!name) {
    return res.status(400).json({ error: 'Name is required' });
  }
  res.json({ message: `Hello, ${name}!` });
});

What will the response be if the client sends { "name": "Alice" } as JSON?

Express
app.post('/greet', (req, res) => {
  const { name } = req.body;
  if (!name) {
    return res.status(400).json({ error: 'Name is required' });
  }
  res.json({ message: `Hello, ${name}!` });
});
A{"error":"Name is required"}
B{"message":"Hello, Alice!"}
CStatus 500 Internal Server Error
DEmpty response with status 200
Attempts:
2 left
💡 Hint

Think about what the route does when name is present in the request body.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a JSON schema middleware for validating a request body in Express?

You want to validate that the request body contains a username (string) and age (number) before processing it. Which middleware definition is correct?

A
const schema = {
  username: 'string',
  age: 'number',
  required: ['username', 'age']
};
B
const schema = {
  type: 'array',
  items: {
    username: 'string',
    age: 'number'
  }
};
C
const schema = {
  type: 'object',
  properties: {
    username: { type: 'string' },
    age: { type: 'number' }
  },
  required: ['username', 'age'],
  additionalProperties: false
};
D
const schema = {
  type: 'object',
  properties: {
    username: 'string',
    age: 'number'
  },
  required: ['username', 'age']
};
Attempts:
2 left
💡 Hint

JSON schema requires type and properties keys with proper types.

🔧 Debug
advanced
2:00remaining
What error does this Express route produce when the request body is missing the required field?

Given this route using a JSON schema validator middleware:

app.post('/user', validateSchema(schema), (req, res) => {
  res.json({ success: true });
});

If the client sends {} (empty body) and schema requires a username, what error will occur?

Express
const schema = {
  type: 'object',
  properties: {
    username: { type: 'string' }
  },
  required: ['username'],
  additionalProperties: false
};
ASyntaxError thrown by Express
B500 Internal Server Error due to missing property
C200 OK with empty JSON response
D400 Bad Request with validation error message
Attempts:
2 left
💡 Hint

Think about how validation middleware handles missing required fields.

state_output
advanced
2:00remaining
What is the response body after this Express route processes a request with extra fields?

Consider this route that validates request body against a schema disallowing extra fields:

const schema = {
  type: 'object',
  properties: {
    email: { type: 'string' }
  },
  required: ['email'],
  additionalProperties: false
};

app.post('/subscribe', validateSchema(schema), (req, res) => {
  res.json({ subscribed: true });
});

If the client sends { "email": "user@example.com", "age": 30 }, what will the response be?

A400 Bad Request with error about additional properties
B200 OK with {"subscribed": true}
C500 Internal Server Error
D200 OK with empty JSON {}
Attempts:
2 left
💡 Hint

Check what additionalProperties: false means in JSON schema.

🧠 Conceptual
expert
2:00remaining
Which statement best describes the role of request and response schemas in Express applications?

Choose the most accurate description of why request and response schemas are used in Express apps.

AThey ensure data sent and received matches expected formats, improving reliability and security.
BThey allow Express to run faster by skipping data parsing.
CThey replace the need for any manual error handling in routes.
DThey automatically generate HTML forms for user input based on schema definitions.
Attempts:
2 left
💡 Hint

Think about the purpose of validating data in web applications.