如题,express中间件,他是如何知道哪个中间件先处理,哪个中间件后处理的?这不用我们管吗?如果我们 有2个自定义中间件有顺序要求,那应该怎么定义呢?
学习是最好的投资!
Suppose the path you request is `/user', and the following two routes match your request at the same time. Then
Theoretically, the middleware in these two route matches will be executed
Whether the subsequent middleware is executed depends on the previous middleware, whether it is called next()
next()
app.get('/user', function(req, res, next){ console.log('1'); next(); }); app.get('/user', function(req, res, next){ console.log('2'); res.send('user'); });
Express internally maintains this order through an array called stack.
stack
xx.stack.push(fn1); xx.stack.push(fn2)
Just write the middleware that is called first in front, it’s that straightforward.
Suppose the path you request is `/user', and the following two routes match your request at the same time. Then
Theoretically, the middleware in these two route matches will be executed
Whether the subsequent middleware is executed depends on the previous middleware, whether it is called
next()
Express internally maintains this order through an array called
stack
.Just write the middleware that is called first in front, it’s that straightforward.