0
0
Expressframework~10 mins

Why input validation is critical in Express - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get user input from the request body in Express.

Express
app.post('/submit', (req, res) => {
  const userInput = req.body.[1];
  res.send('Received');
});
Drag options to blanks, or click blank then click option'
Avalue
Bdata
Cuser
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong key name that does not exist in req.body.
Trying to get input from req.params instead of req.body.
2fill in blank
medium

Complete the code to check if the user input is empty and respond with an error.

Express
app.post('/submit', (req, res) => {
  const input = req.body.input;
  if (input [1] '') {
    return res.status(400).send('Input is required');
  }
  res.send('Input received');
});
Drag options to blanks, or click blank then click option'
A===
B!==
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of === which can cause unexpected results.
Checking for truthiness instead of empty string.
3fill in blank
hard

Fix the error in the input validation to ensure only numbers are accepted.

Express
app.post('/submit', (req, res) => {
  const input = req.body.input;
  if (!Number.isInteger([1])) {
    return res.status(400).send('Input must be an integer');
  }
  res.send('Valid input');
});
Drag options to blanks, or click blank then click option'
AparseInt(input)
BNumber(input)
Cinput
Dinput.length
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'input' directly without conversion, as it's a string.
Passing input.length which is a number but unrelated to input value.
4fill in blank
hard

Fill both blanks to sanitize and validate user input as a trimmed non-empty string.

Express
app.post('/submit', (req, res) => {
  const input = req.body.input[1]();
  if (input [2] '') {
    return res.status(400).send('Input cannot be empty');
  }
  res.send('Input accepted');
});
Drag options to blanks, or click blank then click option'
Atrim
B===
C!==
DtoLowerCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using toLowerCase instead of trim which does not remove spaces.
Using !== instead of === which reverses the logic.
5fill in blank
hard

Fill all three blanks to validate input length and type before processing.

Express
app.post('/submit', (req, res) => {
  const input = req.body.input;
  if (typeof input !== [1] || input.length [2] 0 || input.length [3] 100) {
    return res.status(400).send('Invalid input');
  }
  res.send('Input is valid');
});
Drag options to blanks, or click blank then click option'
A'string'
B>
C<=
D'number'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for type 'number' instead of 'string'.
Using wrong comparison operators that invert the logic.