0
0
Expressframework~10 mins

Why authentication matters in Express - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Express library.

Express
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Afs
Bhttp
Cpath
Dexpress
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different library name like 'http' instead of 'express'.
Forgetting to put the library name inside quotes.
2fill in blank
medium

Complete the code to create a new Express app instance.

Express
const app = [1]();
Drag options to blanks, or click blank then click option'
Ahttp
Bexpress
Crouter
Dserver
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express' to create the app.
Trying to create app without calling a function.
3fill in blank
hard

Fix the error in the middleware that checks if a user is authenticated.

Express
function checkAuth(req, res, next) {
  if (!req.user) {
    return res.status([1]).send('Unauthorized');
  }
  next();
}
Drag options to blanks, or click blank then click option'
A401
B404
C500
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 which means success.
Using 500 which means server error.
Using 404 which means not found.
4fill in blank
hard

Fill both blanks to create a route that requires authentication before sending a response.

Express
app.[1]('/dashboard', [2], (req, res) => {
  res.send('Welcome to your dashboard');
});
Drag options to blanks, or click blank then click option'
Aget
Bpost
CcheckAuth
Dexpress.json()
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET for a page route.
Using unrelated middleware like express.json() instead of checkAuth.
5fill in blank
hard

Fill all three blanks to set up a login route that authenticates user credentials and sends a success message.

Express
app.[1]('/login', (req, res) => {
  const { username, password } = req.[2];
  if (username === 'admin' && password === 'secret') {
    res.[3]('Login successful');
  } else {
    res.status(401).send('Invalid credentials');
  }
});
Drag options to blanks, or click blank then click option'
Apost
Bbody
Csend
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for login.
Reading credentials from req.query instead of req.body.
Using res.status() without send() to respond.