Recall & Review
beginner
What is middleware in Express?
Middleware is a function that runs during the request-response cycle. It can modify the request or response, or end the cycle by sending a response. Middleware helps organize code into reusable steps.
Click to reveal answer
beginner
Why compose multiple middleware functions for authentication?
Composing middleware lets you separate concerns like checking tokens, verifying user roles, and logging. This makes code easier to read, test, and reuse.
Click to reveal answer
beginner
How does Express know to move from one middleware to the next?
Express moves to the next middleware when the current middleware calls the next() function. If next() is not called, the request stops there.
Click to reveal answer
intermediate
What happens if an authentication middleware detects an invalid token?
It usually sends a response with an error status (like 401 Unauthorized) and does not call next(), stopping the request from reaching protected routes.
Click to reveal answer
intermediate
Give an example of composing two middleware functions for auth layers.
You can create one middleware to check if a token exists and another to verify user roles. Then use them together like app.use(checkToken, verifyRole) to protect routes.
Click to reveal answer
What does the next() function do in Express middleware?
✗ Incorrect
Calling next() tells Express to continue to the next middleware or route handler.
Which status code is commonly sent when authentication fails?
✗ Incorrect
401 Unauthorized means the user is not authenticated or token is invalid.
Why use multiple middleware functions for auth instead of one big function?
✗ Incorrect
Modular middleware separates concerns and improves code clarity.
If a middleware does not call next() or send a response, what happens?
✗ Incorrect
Without next() or a response, the request stays open and the client waits indefinitely.
How do you apply multiple middleware functions to a route in Express?
✗ Incorrect
You pass multiple middleware functions as arguments to app.use or route methods.
Explain how middleware composition helps in building authentication layers in Express.
Think about breaking down auth steps into small functions.
You got /4 concepts.
Describe what happens when an Express middleware detects an invalid token during authentication.
Focus on the flow control and response.
You got /4 concepts.