0
0
Expressframework~10 mins

Manual validation patterns 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 Express and create an app instance.

Express
const express = require('[1]');
const app = express();
Drag options to blanks, or click blank then click option'
Aexpress
Bhttp
Cfs
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express' for the require statement.
Forgetting to require the Express module.
2fill in blank
medium

Complete the code to add a middleware that parses JSON bodies.

Express
app.use(express.[1]());
Drag options to blanks, or click blank then click option'
Ajson
Burlencoded
Cstatic
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'urlencoded' middleware instead of 'json' for JSON bodies.
Not adding any middleware to parse request bodies.
3fill in blank
hard

Fix the error in the validation middleware to check if 'age' is a number.

Express
app.post('/submit', (req, res) => {
  const age = req.body.age;
  if (typeof age !== '[1]') {
    return res.status(400).send('Age must be a number');
  }
  res.send('Valid age');
});
Drag options to blanks, or click blank then click option'
Astring
Bboolean
Cnumber
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'string' type instead of 'number'.
Using 'typeof age === number' without quotes.
4fill in blank
hard

Fill both blanks to validate that 'email' exists and contains '@'.

Express
app.post('/register', (req, res) => {
  const email = req.body.email;
  if (!email || !email[1]('@')) {
    return res.status(400).send('Invalid email');
  }
  res.send('Email valid');
});
Drag options to blanks, or click blank then click option'
AindexOf
Bincludes
CstartsWith
DendsWith
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'startsWith' or 'endsWith' which only check start or end of string.
Using 'indexOf' but forgetting to check for -1.
5fill in blank
hard

Fill both blanks to validate 'username' length and characters.

Express
app.post('/user', (req, res) => {
  const username = req.body.username;
  if (!username || username.length < [1] || !/^[a-zA-Z0-9]+$/.[2](username)) {
    return res.status(400).send('Invalid username');
  }
  res.send('Username valid');
});
Drag options to blanks, or click blank then click option'
A3
Btest
Cmatch
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'match' instead of 'test' for regex validation.
Checking length with wrong number.