Recall & Review
beginner
What is a middleware factory in Express?
A middleware factory is a function that returns a middleware function. It allows you to create middleware with custom settings or parameters.
Click to reveal answer
beginner
Why use a middleware factory instead of a simple middleware function?
Middleware factories let you customize behavior by passing arguments when creating the middleware, making your code reusable and flexible.Click to reveal answer
intermediate
Example: What does this middleware factory do?
<pre>function logger(level) {
return function(req, res, next) {
console.log(`[${level}] ${req.method} ${req.url}`);
next();
};
}</pre>This factory creates a logger middleware that logs the HTTP method and URL with a custom log level like 'INFO' or 'DEBUG'.
Click to reveal answer
beginner
How do you apply a middleware created by a factory in Express?
Call the factory with parameters to get the middleware, then use it with app.use or route handlers, e.g., app.use(logger('INFO')).
Click to reveal answer
intermediate
What is the benefit of middleware factories for code maintenance?
They keep code DRY (Don't Repeat Yourself) by letting you write one factory and create many customized middleware instances from it.
Click to reveal answer
What does a middleware factory return in Express?
✗ Incorrect
A middleware factory returns a middleware function that Express can use in the request pipeline.
Why might you pass parameters to a middleware factory?
✗ Incorrect
Parameters let you customize what the middleware does, like setting log levels or options.
How do you use a middleware factory in an Express app?
✗ Incorrect
You call the factory to get middleware, then pass it to app.use() or route handlers.
Which of these is NOT a benefit of middleware factories?
✗ Incorrect
Middleware factories do not automatically handle database queries; they help create middleware functions.
What is the main difference between a middleware factory and a regular middleware?
✗ Incorrect
A middleware factory returns a middleware function, while regular middleware is the function used directly.
Explain how a middleware factory works in Express and why it is useful.
Think about how you can create middleware that changes behavior based on input.
You got /4 concepts.
Describe a simple example of a middleware factory and how you would use it in an Express app.
Imagine you want to log requests with different levels.
You got /4 concepts.