0

express-ip-access-control用来做 ACL 检查。

我的中间件功能是这样的:

const ipAccessControl = require('express-ip-access-control')

function accessControl (req, res, next) {
    ipAccessControl(someOptions)(req, res, next)

    // additional ACL checking logic
    ...
    switch(result) {
        case ALLOW:
            next()
            break
        case DENY:
            res.status(400).send("denied")
    }
}

那个 ipAccessControl 工作得很好,但是当 ipAccessControl 允许访问并调用 next() 时,我不知道如何返回。

上面的代码总是同时执行 ipAccessControl 和额外的 ACL 检查逻辑。

有没有办法检查 ipAccessControl 的结果,并且只在 ipAccessControl 调用时返回next()

当 ipAccessControl 允许访问时,我不希望调用我的附加检查逻辑。

应该在附加 ACL 检查逻辑之前调用 ipAccessControl。

任何想法表示赞赏。

4

1 回答 1

1

你可以传递你自己的函数来代替,next()然后你就会知道它什么时候完成:

const ipAccessControl = require('express-ip-access-control')

function accessControl (req, res, next) {
    ipAccessControl(someOptions)(req, res, function(err) {
        if (err) {
            // call the actual next with the error
            return next(err);
        } else {
            // additional ACL checking logic
            ...
            switch(result) {
                case ALLOW: ...
                case DENY: ...
            }
            // when done successfully, call next() here
            // or if there's an error, then send an error response
            // or call next(err)
        }
    })

}
于 2020-12-08T05:56:19.090 回答