Recall & Review
beginner
What is the purpose of auth middleware in Express?
Auth middleware checks if a user is logged in before allowing access to certain routes. It protects routes from unauthorized users.
Click to reveal answer
beginner
How do you apply auth middleware to a specific route in Express?
You add the middleware function as a second argument in the route definition, like:
app.get('/dashboard', authMiddleware, (req, res) => {...}).Click to reveal answer
beginner
What should auth middleware do if the user is not authenticated?
It should stop the request and respond with a status like 401 Unauthorized or redirect the user to a login page.
Click to reveal answer
intermediate
Why is middleware a good way to protect routes?
Middleware lets you reuse the same auth check on many routes without repeating code. It keeps your code clean and organized.
Click to reveal answer
beginner
Show a simple example of auth middleware in Express.
A simple auth middleware checks if <code>req.user</code> exists. If yes, it calls <code>next()</code> to continue. If no, it sends a 401 response.<br><br><code>function authMiddleware(req, res, next) {<br> if (req.user) {<br> next();<br> } else {<br> res.status(401).send('Unauthorized');<br> }<br>}</code>Click to reveal answer
What does auth middleware typically check before allowing access to a route?
✗ Incorrect
Auth middleware checks if the user is logged in or authenticated before allowing access.
How do you add middleware to protect a route in Express?
✗ Incorrect
Middleware is added as a second argument before the route handler to protect the route.
What should auth middleware do if the user is not authenticated?
✗ Incorrect
If the user is not authenticated, middleware should stop the request and send a 401 Unauthorized response.
Why is using middleware good for protecting many routes?
✗ Incorrect
Middleware allows reusing the same auth logic on many routes, keeping code clean.
Which Express method is used to continue to the next middleware or route handler?
✗ Incorrect
Calling next() passes control to the next middleware or route handler.
Explain how auth middleware protects routes in Express and why it is useful.
Think about how middleware acts like a gatekeeper before route handlers.
You got /4 concepts.
Write a simple auth middleware function for Express and describe what it does.
Focus on the basic structure and the decision to allow or block access.
You got /4 concepts.