Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Express module correctly.
Express
const express = require([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong module name like 'express-session' or 'body-parser'.
Forgetting to put the module name in quotes.
✗ Incorrect
You need to import the main Express module using require("express") to create an Express app.
2fill in blank
mediumComplete the code to set up a POST route for login at '/login'.
Express
app.[1]('/login', (req, res) => { // login logic });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for login routes.
Using PUT or DELETE which are not typical for login.
✗ Incorrect
Login forms usually send data via POST, so use app.post to handle the login route.
3fill in blank
hardFix the error in the middleware usage to parse JSON bodies.
Express
app.use([1].json()); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using body-parser.json() without importing body-parser.
Using express-session or cookie-parser which are unrelated.
✗ Incorrect
Express has a built-in json() middleware accessed via express.json() to parse JSON request bodies.
4fill in blank
hardFill both blanks to check if a user is authenticated and redirect if not.
Express
function ensureAuth(req, res, next) {
if (req.[1]) {
next();
} else {
res.[2]('/login');
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.send instead of res.redirect for navigation.
Checking a wrong property on req object.
✗ Incorrect
The method req.isAuthenticated() checks if the user is logged in, and res.redirect() sends the user to the login page if not.
5fill in blank
hardFill all three blanks to create a test that sends a POST request to login and checks the response status.
Express
test('POST /login returns 200 on success', async () => { const response = await request(app) .[1]('/login') .send({ username: 'user', password: 'pass' }); expect(response.[2]).toBe([3]); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using get() instead of post() for login.
Checking response.statusCode instead of response.status.
Expecting a wrong status code like 404.
✗ Incorrect
Use post() to send the login request, check response.status, and expect it to be 200 for success.