Reputation: 13344
Does anyone know how to implement the function chain used in connect/express. So one can do this..
var app = {}, app.stack = [];
app.use(function(r, s, n){
// dosomething
})
require('http').createServer(function(r, s){
// execute functions in app stack
})
Upvotes: 0
Views: 127
Reputation: 146124
The middleware "chain" is actually just a "stack" which is actually a simple javascript Array of functions to execute in order. Whenever you call use
, connect appends your function to the middleware stack. When it's time to run middleware, connect just executes all the functions in order with a bit of logic to pass the req, res, next
parameters and wire up the next
callback to mean continue processing the middleware stack. I agree with @robertklep that you should read the source code as it is quite readable and illuminating.
Upvotes: 1