简体   繁体   English

Express中间件功能链中的控制流返回如何工作?

[英]How does the return of control flow work in a chain of Express middleware functions?

I actually have a couple of questions about Express middleware chaining and thought I'd group them in this single post since they're closely related. 实际上,我对Express中间件链接有几个问题,并认为我将它们归为一类,因为它们密切相关。


Q1: In a middleware function, if I send() the Response , pass to the next function, or just return, does any of the remaining code get executed? 问题1:在中间件函数中,如果我send() Response send() ,传递给下一个函数,或者仅返回,是否执行了其余代码?

router.get('/a', (req, res, next) => {
    res.sendStatus(200);
    // Does this ever get executed?
}); 

router.get('/b', (req, res, next) => {
    next();
    // Does this ever get executed?
}); 

router.get('/c', (req, res, next) => {
    return;
    // Does this stop the middleware chain? What happens to the request/response?
});

Q2: Can you pass on to another middleware function from inside of the preceding one? Q2:可以从上一个内部函数传递到另一个中间件函数吗? My reason for this is that I have to supply an argument to create a certain middleware, and that argument is dependent on which Express app object is the owner of the Request . 我这样做的原因是,我必须提供一个参数来创建某个中间件,并且该参数取决于哪个Express app对象是Request的所有者。 So I need to access a property of the Request before it is passed to the middleware that needs to be created using that property. 因此,在将请求的属性传递给需要使用该属性创建的中间件之前,我需要访问该属性。 Would this work: ? 这项工作会:

router.get('/d', (req, res, next) => {
    var middlewareFunction = createDynamicMiddleware();
    middlewareFunction(req, res, next); // Will this still work as expected?
});

Q3: How are the Request / Response arguments passed to the next middleware when you call next() without any arguments? Q3:当您不带任何参数调用next()时, Request / Response参数如何传递给下一个中间件?

Q1 : In a middleware function, if I send() the Response, pass to the next function, or just return, does any of the remaining code get executed? 问题1 :在中间件函数中,如果我将Response发送给send(),传递给下一个函数,或者仅返回,是否执行了其余的代码?

router.get('/a', (req, res, next) => {
    res.sendStatus(200);
    // Does this ever get executed?
    //    Yes, but don't call next() or res.send() or it will throw an error

}); 

router.get('/b', (req, res, next) => {
    next();
    // Does this ever get executed?
    //    Yes, but don't call next() or res.send() or it will throw an error

}); 

router.get('/c', (req, res, next) => {
    return;
    // Does this stop the middleware chain? What happens to the request/response?
    //    The request get stuck and after a while it will fail due 'timeout'
});

Q2 : Can you pass on to another middleware function from inside of the preceding one? Q2 :可以从上一个内部函数传递到另一个中间件函数吗? My reason for this is that I have to supply an argument to create a certain middleware, and that argument is dependent on which Express app object is the owner of the Request. 我这样做的原因是,我必须提供一个参数来创建某个中间件,并且该参数取决于哪个Express应用程序对象是Request的所有者。 So I need to access a property of the Request before it is passed to the middleware that needs to be created using that property. 因此,在将请求的属性传递给需要使用该属性创建的中间件之前,我需要访问该属性。

Your factory of dynamic middleware should return a new function 您的动态中间件工厂应返回新功能

Functions that returns new functions, are also knows as curried functions, here is a practical example: 返回新函数的函数也称为咖喱函数,这是一个实际示例:

// based on 'someId' we will return a specific middleware

req.get('/user/:someId', 
 createDynamicMiddleware,
 (req,res) => { 
  res.send('ok') 
 })

function createDynamicMiddleware(req,res,next){
  if(req.params.someId === 'me') {
    return function(req, res, next){
     // do some stuff if someId is me
     return next();
    }
  } else {
    return function(req, res, next){
     // do some other stuff otherwise
     return next();
    }
  }
}

Q3 : How are the Request/Response arguments passed to the next middleware when you call next() without any arguments? Q3 :当您不带任何参数调用next()时,请求/响应参数如何传递给下一个中间件?

Express handle this for you! 快递为您处理!

Also, if you add a property to req or res objects, it will be present in the next middlewares 另外,如果将属性添加到req或res对象,则该属性将出现在下一个中间件中

For example: 例如:

router.get('/', (req,res,next) => {
  req.customProperty = 'hello!';
  return next();
}, (req, res) => {
  console.log(req.myCustomProperty)
  // hello!
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM