In this rendr - sessions example, there's an express middleware module...
在这个rendr - sessions示例中,有一个快速的中间件模块......
module.exports = function incrementCounter() {
return function incrementCounter(req, res, next) {
var app = req.rendrApp
, count = app.get('session').count || 0;
req.updateSession('count', count + 1);
next();
};
};
Can you not achieve the same thing with the following?
以下是否可以达到同样的目的?
module.exports = function incrementCounter(req, res, next) {
var app = req.rendrAp
, count = app.get('session').count || 0;
req.updateSession('count', count + 1);
next();
};
My Question is, why would you export a function which returns a function with arguments? Is there some sort of benefit to the former that I am unaware of?
我的问题是,为什么要导出一个返回带参数函数的函数?我不知道前者会有某些好处吗?
1 个解决方案
#1
rendr uses Express-style middleware.
rendr使用Express风格的中间件。
By convention, third-party middleware in Express are not provided as the actual middleware. Instead, they are provided as functions that create the middleware based on an options object parameter.
按照惯例,Express中的第三方中间件不作为实际的中间件提供。相反,它们是作为基于选项对象参数创建中间件的函数提供的。
However, since there are no options to be provided here, it is omitted.
但是,由于此处没有提供选项,因此省略。
But still, in order to follow the conventions of the surrounding library, it needs to be a factory function that returns a middleware function. So that's why it is wrapped up that way here.
但是,为了遵循周围库的约定,它需要是一个返回中间件函数的工厂函数。所以这就是为什么它在这里被包裹起来的原因。
#1
rendr uses Express-style middleware.
rendr使用Express风格的中间件。
By convention, third-party middleware in Express are not provided as the actual middleware. Instead, they are provided as functions that create the middleware based on an options object parameter.
按照惯例,Express中的第三方中间件不作为实际的中间件提供。相反,它们是作为基于选项对象参数创建中间件的函数提供的。
However, since there are no options to be provided here, it is omitted.
但是,由于此处没有提供选项,因此省略。
But still, in order to follow the conventions of the surrounding library, it needs to be a factory function that returns a middleware function. So that's why it is wrapped up that way here.
但是,为了遵循周围库的约定,它需要是一个返回中间件函数的工厂函数。所以这就是为什么它在这里被包裹起来的原因。