Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Express framework.
Node.js
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express' to import the framework.
✗ Incorrect
The Express framework is imported by requiring 'express'.
2fill in blank
mediumComplete the code to use the express.json() middleware for parsing JSON input.
Node.js
app.use([1]()); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using express.urlencoded() which parses URL-encoded data instead of JSON.
✗ Incorrect
express.json() middleware parses incoming JSON requests.
3fill in blank
hardFix the error in the validation code to check if the input 'age' is a number.
Node.js
if (typeof [1] !== 'number') { res.status(400).send('Age must be a number'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.params.age or req.query.age when input is sent in JSON body.
✗ Incorrect
Input data from JSON body is accessed via req.body.age.
4fill in blank
hardFill both blanks to sanitize and validate a username input using the 'validator' library.
Node.js
const sanitizedUsername = validator.[1](req.body.username); if (!validator.[2](sanitizedUsername)) { res.status(400).send('Invalid username'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using isEmail() to validate username which is incorrect.
✗ Incorrect
Use escape() to sanitize input and isAlphanumeric() to validate username characters.
5fill in blank
hardFill all three blanks to create a middleware that validates and sanitizes an email input.
Node.js
function validateEmail(req, res, next) {
const email = validator.[1](req.body.email);
if (!validator.[2](email)) {
return res.status(400).send('Invalid email');
}
req.body.email = email[3]();
next();
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using escape() on email which can break the format.
✗ Incorrect
normalizeEmail() cleans the email, isEmail() validates it, and trim() removes extra spaces.