【发布时间】:2016-01-01 21:51:51
【问题描述】:
下面有一些代码,我在我的 Express.js 应用程序中使用这些代码来集中一些 acl 逻辑。如果函数显式返回true 或false,则中间件可以处理next 调用。但是,如果它没有返回,则取决于授权逻辑在完成操作时执行next()。
为了避免写出错误数据,我只想传入一个可以调用的error()函数,它只是在内部调用next函数。
有人告诉我这可能会导致某种内存泄漏,因为next 函数位于它自己的闭包中并从外部引用它。我在网上看到很多示例中都使用了类似的技术,但我对 Node.js 还是很陌生,所以想知道这是否有任何道理?
this.router.use(function (req, res, next) {
var err = {
code: 403,
exception: 'UnauthorizedException',
data: {}
},
error = function () {
next(err);
},
authorize = app.route.authorize(req, res, next, error);
if (authorize === false) {
next(err);
}
else if (authorize === true) {
next();
}
});
编辑:删除变量
this.router.use(function (req, res, next) {
var authorize = app.route.authorize(req, res, next, function () {
next({
code: 403,
exception: 'UnauthorizedException',
data: {}
});
});
if (authorize === false) {
next({
code: 403,
exception: 'UnauthorizedException',
data: {}
});
}
else if (authorize === true) {
next();
}
});
【问题讨论】:
-
我能问一下您为什么要使用授权方法来处理
next调用吗?这在什么情况下有用?
标签: javascript node.js express memory-leaks