Express middleware, how does he know which middleware to process first and which middleware to process later?

  node.js, question

For example, express middleware, how does he know which middleware to process first and which middleware to process later? This is not our tube? If we have 2 custom middleware with sequence requirements, how should we define them?

Let’s assume that the path you requested is ` /user’, and at this time there are two routes matching your request at the same time. Then

  1. In theory, the middleware in both routing matches will execute

  2. Whether the latter middleware is executed depends on whether the former middleware has been called or not.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 is built internally through a system calledstackTo maintain this order.

xx.stack.push(fn1);
 xx.stack.push(fn2)