0
0
Expressframework~10 mins

Validating body fields in Express - Interactive Code Practice

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

Complete the code to import the Express library.

Express
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Aexpress
Bbody-parser
Chttp
Dfs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'body-parser' instead of 'express'.
Using 'http' which is a Node.js core module.
2fill in blank
medium

Complete the code to parse JSON body data in Express.

Express
app.use(express.[1]());
Drag options to blanks, or click blank then click option'
Aurlencoded
Bjson
Ctext
Draw
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'urlencoded' when expecting JSON data.
Forgetting to add the middleware.
3fill in blank
hard

Fix the error in the route handler to access the 'username' field from the request body.

Express
app.post('/login', (req, res) => {
  const username = req.body[1];
  res.send(`Hello, ${username}`);
});
Drag options to blanks, or click blank then click option'
A.params.username
B.query.username
C['username']
D.username
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.params or req.query instead of req.body.
Missing quotes in bracket notation.
4fill in blank
hard

Fill both blanks to validate that the 'email' field exists and is a string.

Express
if (typeof req.body[1] === '[2]') {
  res.send('Valid email');
} else {
  res.status(400).send('Invalid email');
}
Drag options to blanks, or click blank then click option'
A['email']
Bemail
Cstring
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation without quotes in the first blank.
Checking for 'number' instead of 'string'.
5fill in blank
hard

Fill all three blanks to create a middleware that checks if 'password' exists and is at least 6 characters long.

Express
function validatePassword(req, res, next) {
  const pwd = req.body[1];
  if (typeof pwd === '[2]' && pwd.length [3] 6) {
    next();
  } else {
    res.status(400).send('Password too short');
  }
}
Drag options to blanks, or click blank then click option'
A['password']
Bpassword
C>=
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation without quotes for password.
Checking length with '<' instead of '>='.
Checking type as 'number' instead of 'string'.