0
0
Expressframework~10 mins

Testing authentication flows 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 the Express module correctly.

Express
const express = require([1]);
Drag options to blanks, or click blank then click option'
A"body-parser"
B"express-session"
C"express"
D"cookie-parser"
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.
2fill in blank
medium

Complete 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'
Aget
Bpost
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for login routes.
Using PUT or DELETE which are not typical for login.
3fill in blank
hard

Fix 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'
Aexpress
Bbody-parser
Ccookie-parser
Dexpress-session
Attempts:
3 left
💡 Hint
Common Mistakes
Using body-parser.json() without importing body-parser.
Using express-session or cookie-parser which are unrelated.
4fill in blank
hard

Fill 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'
AisAuthenticated()
Bredirect
Csend
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.send instead of res.redirect for navigation.
Checking a wrong property on req object.
5fill in blank
hard

Fill 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'
Apost
Bstatus
C200
Dget
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.