Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express' for the require statement.
Forgetting to require the Express module.
✗ Incorrect
You need to import the 'express' module to create an Express app.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'urlencoded' middleware instead of 'json' for JSON bodies.
Not adding any middleware to parse request bodies.
✗ Incorrect
Use express.json() middleware to parse JSON request bodies.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'string' type instead of 'number'.
Using 'typeof age === number' without quotes.
✗ Incorrect
Use typeof age === 'number' to check if age is a number.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Use email.includes('@') to check if the email contains '@'.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'match' instead of 'test' for regex validation.
Checking length with wrong number.
✗ Incorrect
Check username length is at least 3 and use regex test method to validate characters.