0
0
Node.jsframework~5 mins

Building custom middleware in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is middleware in Node.js 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.
Click to reveal answer
beginner
How do you define a custom middleware function in Express?
A custom middleware is a function with three parameters: req, res, and next. It processes the request and calls next() to pass control to the next middleware.
Click to reveal answer
beginner
What does the next() function do in middleware?
next() passes control to the next middleware or route handler. Without calling next(), the request will hang and not proceed.
Click to reveal answer
intermediate
How can middleware modify the request object?
Middleware can add new properties or change existing ones on req. For example, adding req.user after authentication.
Click to reveal answer
intermediate
Where should you place custom middleware in your Express app?
Place middleware before the routes that need it. This ensures the middleware runs first and can affect those routes.
Click to reveal answer
What are the three parameters of an Express middleware function?
Arequest, response, finish
Brequest, response, callback
Creq, res, done
Dreq, res, next
What happens if you forget to call next() in your middleware?
AThe server will crash immediately.
BThe request will hang and never reach the next middleware or route.
CThe response will automatically be sent.
DThe middleware will skip to the last route.
Which of these is a common use case for custom middleware?
ALogging requests
BRendering HTML templates
CConnecting to a database
DServing static files
Where should you place middleware in your Express app?
ABefore the routes that need it
BAfter all routes
COnly inside route handlers
DAnywhere, order does not matter
How can middleware add data to be used later in the request cycle?
ABy modifying the <code>res</code> headers only
BBy sending a response immediately
CBy adding properties to the <code>req</code> object
DBy calling <code>next()</code> twice
Explain how to create and use a custom middleware function in Express.
Think about the function signature and where it fits in the request flow.
You got /4 concepts.
    Describe what happens if you do not call next() inside your middleware.
    Consider the flow of control in Express middleware.
    You got /3 concepts.