Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Express library.
Node.js
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using built-in modules like 'http' instead of 'express'.
✗ Incorrect
The Express library is imported using require('express').
2fill in blank
mediumComplete the code to create a new Express application instance.
Node.js
const app = [1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a non-existent function like 'http()'.
✗ Incorrect
You create an Express app by calling express().
3fill in blank
hardFix the error in the middleware function to correctly parse JSON requests.
Node.js
app.use(express.[1]()); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'urlencoded' when expecting JSON data.
✗ Incorrect
To parse JSON request bodies, use express.json() middleware.
4fill in blank
hardFill both blanks to validate that the request body has a 'name' property before proceeding.
Node.js
app.post('/submit', (req, res, next) => { if (!req.body.[1]) { return res.status([2]).send('Name is required'); } next(); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'username' instead of 'name'.
Using 404 instead of 400 status code.
✗ Incorrect
The code checks if req.body.name exists. If not, it sends a 400 Bad Request status.
5fill in blank
hardFill all three blanks to create a middleware that validates 'email' and 'password' fields in the request body.
Node.js
function validateUser(req, res, next) {
const { [1], [2] } = req.body;
if (![1] || ![2]) {
return res.status(400).send('Email and password are required');
}
next();
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'username' or 'token' instead of 'email' and 'password'.
✗ Incorrect
The middleware extracts email and password from req.body and checks if both exist before continuing.