简体   繁体   English

在自定义中间件中使用Connect \\ Express中间件

[英]Using Connect\Express middleware within custom middleware

Is it viable to use popular Connect middleware within your custom middleware? 在自定义中间件中使用流行的Connect中间件是否可行?

For example, I'm writing some authentication middleware, which obviously relies quite heavily on Connect's cookieParser() and session methods. 例如,我正在编写一些身份验证中间件,该中间件显然在很大程度上依赖于Connect的cookieParser()session方法。 These methods are middleware, so they require request , response and next parameters to be passed. 这些方法是中间件,因此它们需要传递requestresponsenext参数。 The obvious option is to simply make sure I'm adding them to the middleware stack before I add my authentication middleware like so: 显而易见的选择是简单地确保在添加身份验证中间件之前,先将它们添加到中间件堆栈中,如下所示:

app.js: app.js:

app.use(express.cookieParser('secret'))
   .use(express.session({ secret: 'keyboard cat', key: 'sid', cookie: { secure: true }}))
   .use(my_auth_middleware())

But this seems a bit cumbersome as my middleware relies on the first two methods in order to do stuff with the req.session . 但这似乎有点麻烦,因为我的中间件依靠前两种方法来处理req.session

The other obvious way to do it would be to pass the app into my middleware, and then call the cookieParser() and session methods within, but because they are both middleware I would have to add them to the stack, which feels wrong: 另一个明显的实现方法是将应用程序传递到我的中间件中,然后在其中调用cookieParser()session方法,但是由于它们都是中间件,因此我不得不将它们添加到堆栈中,这是错误的:

my_auth_middleware.js: my_auth_middleware.js:

module.exports = function(app){

    app.use(express.cookieParser('secret'));
    app.use(express.session({ secret: 'keyboard cat', key: 'sid', cookie: { secure: true }}));

    return function(req, res, next){

        // do stuff with req.session

        next();

    }
}

Can anyone confirm that this is a logical way to do things? 谁能确认这是做事的逻辑方法? Is there an argument for keeping the cookieParser() and session methods out of my middleware? 是否有用于将cookieParser()session方法保留在中间件之外的参数?

Obviously I'm using Express in these examples, but I'm aware these methods originate from Connect. 显然,我在这些示例中使用Express,但是我知道这些方法源自Connect。

I don't think there's anything wrong with your first setup. 我认为您的首次设置没有任何问题。 It's rather explicit (you could perhaps add a comment stating that my_auth_middleware() relies on the other two), and therefore pretty obvious to anyone looking at your code. 这是相当明确的(您可能可以添加一条注释,指出my_auth_middleware()依赖于其他两个),因此对于任何查看您的代码的人来说都是显而易见的。

Your second example almost hides the fact that the other two middleware are being used. 您的第二个示例几乎掩盖了正在使用其他两个中间件的事实。 They also move some of your applications' configuration (secrets and cookie name) to a separate file, which might be confusing. 他们还将您的某些应用程序配置(机密和cookie名称)移到一个单独的文件中,这可能会造成混淆。 And personally I don't like passing app around. 我个人不喜欢传递app

FWIW, express.session also needs express.cookieParser to work, but it leaves it up to the programmer to load it. FWIW, express.session还需要express.cookieParser才能工作,但它留给程序员来加载。

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

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