...
/Solution: Factory for Dynamically Composed Middleware Chains
Solution: Factory for Dynamically Composed Middleware Chains
Dynamically compose and return a chain of middleware classes that invoke .handle(req, next) in order.
We'll cover the following...
We'll cover the following...
Solution explanation
Lines 1–26: We define three middleware classes:
LoggerMiddleware,AuthMiddleware, andRateLimitMiddleware.Each exposes an async
.handle(req, next)method, modifying the request and then invokingnext()to continue the chain.This shared interface ensures all middleware can be composed dynamically.
Lines 28–32: The
middlewareMapobject links config keys ('logger','auth','rateLimit') to their respective classes.This allows the factory to instantiate the correct middleware based on the config array without branching. ...