0
0
Expressframework~5 mins

Application-level middleware in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is application-level middleware in Express?
Application-level middleware is a function that runs during the request-response cycle for all or specific routes in an Express app. It can modify requests, responses, or end the request.
Click to reveal answer
beginner
How do you add application-level middleware in Express?
Use app.use() to add middleware that applies to all routes, or add middleware as additional arguments to specific route handlers like app.get('/path', middleware, handler).
Click to reveal answer
beginner
What are the three arguments of an Express middleware function?
The middleware function receives req (request), res (response), and next (a function to pass control to the next middleware).
Click to reveal answer
intermediate
What happens if you don’t call next() in middleware?
The request will hang because Express won’t move to the next middleware or route handler. You must call next() unless you send a response.
Click to reveal answer
intermediate
Can application-level middleware modify the request or response objects?
Yes, middleware can add properties or change req and res objects to share data or customize behavior for later middleware or routes.
Click to reveal answer
Which method adds application-level middleware to all routes in Express?
Aapp.listen()
Bapp.use()
Capp.route()
Dapp.get()
What is the purpose of the next argument in middleware?
ATo send a response to the client
BTo log the request
CTo pass control to the next middleware or route handler
DTo end the server
If middleware does not call next() or send a response, what happens?
AThe request hangs and never finishes
BThe server crashes
CThe request completes successfully
DThe middleware runs twice
How can you apply middleware only to a specific route?
AUse <code>app.listen()</code> with middleware
BUse <code>app.use()</code> with the route path
CMiddleware cannot be route-specific
DAdd middleware as an argument before the route handler, e.g., <code>app.get('/path', middleware, handler)</code>
Which of these is NOT a typical use of application-level middleware?
AHandling database connections
BParsing JSON bodies
CLogging requests
DServing static files
Explain how application-level middleware works in Express and how you add it to your app.
Think about how middleware fits between request and response.
You got /5 concepts.
    Describe what happens if you forget to call next() in your middleware function.
    Imagine the request is stuck waiting for something.
    You got /4 concepts.