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?
✗ Incorrect
Express middleware functions always receive
req, res, and next as parameters.What happens if you forget to call
next() in your middleware?✗ Incorrect
Without calling
next(), Express does not know to continue, so the request hangs.Which of these is a common use case for custom middleware?
✗ Incorrect
Logging requests is a typical middleware task to track incoming traffic.
Where should you place middleware in your Express app?
✗ Incorrect
Middleware must be placed before routes to affect those routes.
How can middleware add data to be used later in the request cycle?
✗ Incorrect
Middleware can add properties to
req so later middleware or routes can use that data.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.