0
0
Expressframework~10 mins

Middleware factory pattern in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Middleware factory pattern
Call factory function with config
Factory returns middleware function
Express uses middleware in request flow
Middleware runs with access to config
Next middleware or response
The factory function creates a middleware configured by input, which Express then runs during requests.
Execution Sample
Express
function logger(level) {
  return function(req, res, next) {
    console.log(`[${level}] ${req.method} ${req.url}`);
    next();
  };
}
app.use(logger('INFO'));
Creates a logger middleware with a level, logs request info, then calls next middleware.
Execution Table
StepActionInput/StateOutput/Result
1Call logger factory with 'INFO'level = 'INFO'Returns middleware function with level='INFO'
2Express receives request GET /homereq.method='GET', req.url='/home'Middleware function called with req, res, next
3Middleware logs messagelevel='INFO', req.method='GET', req.url='/home'Console logs: [INFO] GET /home
4Middleware calls next()next functionExpress continues to next middleware or route handler
5Request handled or response sentNo more middlewareResponse sent to client
💡 Middleware calls next(), allowing Express to continue request processing or send response.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
levelundefined'INFO''INFO''INFO''INFO'
middleware functionundefinedfunction(req,res,next)function(req,res,next)function(req,res,next)function(req,res,next)
req.methodundefinedundefined'GET''GET''GET'
req.urlundefinedundefined'/home''/home''/home'
Key Moments - 2 Insights
Why does the factory function return another function instead of doing the work immediately?
The factory returns a middleware function so Express can call it later with request info. See step 1 and 2 in execution_table where the factory returns a function, and Express calls it with req, res, next.
What happens if the middleware does not call next()?
Express will stop processing further middleware or routes, causing the request to hang. Step 4 shows calling next() lets Express continue.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is logged to the console at step 3?
A[ERROR] GET /home
B[INFO] GET /home
C[INFO] POST /home
D[DEBUG] GET /home
💡 Hint
Check the 'Output/Result' column in step 3 of execution_table.
At which step does Express call the middleware function returned by the factory?
AStep 4
BStep 1
CStep 2
DStep 5
💡 Hint
Look at the 'Action' column describing when middleware is called.
If the factory was called with 'ERROR' instead of 'INFO', what would change in the execution_table?
AThe logged message at step 3 would show [ERROR] instead of [INFO]
BThe middleware would not be called
CThe request method would change
DNothing would change
💡 Hint
Refer to the 'Input/State' and 'Output/Result' columns at step 3.
Concept Snapshot
Middleware factory pattern:
- A function that takes config and returns middleware
- Middleware runs later with req, res, next
- Allows customizing middleware behavior
- Must call next() to continue Express flow
- Example: logger(level) returns middleware logging requests
Full Transcript
The middleware factory pattern in Express means writing a function that takes some settings and returns a middleware function. This returned middleware runs when Express handles a request. For example, a logger factory takes a level like 'INFO' and returns a middleware that logs each request with that level. When a request comes in, Express calls the middleware with request and response objects. The middleware logs the message and calls next() to let Express continue. This pattern helps customize middleware behavior easily. Key points: the factory returns a function, middleware runs later, and next() must be called to avoid stopping the request.