0
0
Expressframework~5 mins

Middleware composition for auth layers in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AEnds the request and sends a response
BMoves to the next middleware in the stack
CRestarts the server
DLogs the request details
Which status code is commonly sent when authentication fails?
A500 Internal Server Error
B200 OK
C404 Not Found
D401 Unauthorized
Why use multiple middleware functions for auth instead of one big function?
ATo make code modular and easier to maintain
BTo slow down the server
CTo confuse developers
DTo avoid using next()
If a middleware does not call next() or send a response, what happens?
AThe server crashes
BExpress automatically calls next()
CThe request hangs and never finishes
DThe browser reloads
How do you apply multiple middleware functions to a route in Express?
Aapp.use(middleware1, middleware2)
Bapp.get(middleware1 + middleware2)
Capp.route(middleware1, middleware2)
Dapp.listen(middleware1, middleware2)
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.