繁体   English   中英

我想知道在第一种情况下,返回的 function 被称为中间件,但在第二种情况下,返回的 function 未被调用

[英]I want to know in the 1st case the returned function is called as a middleware,,but in the 2nd case the returned function is not called

CASE1... 其中 validatebody(schemas.authschema) 返回的 function 作为中间件执行...

router.route("/signup").post(validatebody(schemas.authschema),userscontrollers.signup);

我的第二个文件导出 function 和验证器模式......

module.exports={
validatebody:function(schema)
{
    return function(req,res,next)
    {
        var result=joi.validate(req.body,schema);
        if(result.error)
          return res.status(400).json(result.error);

        
        if(!req.value)
            req.value={};
        
        req.value['body']=result.value;
        next();
    }
},

schemas:{
    authschema:joi.object().keys(
        {
               email:joi.string().email().required(),
               password:joi.string().required()
        })
}
};

现在是第二个案例....在这种情况下,我没有 output 任何东西,并且页面持续加载很长时间,因为 function 名称第二个 ZC1C425268E68385D1AB5074C17A94F14 永远不会被触发并且...但是。 .返回的function被调用..

app.get("/users",firstfunction,secondfunction);


function firstfunction()
{
    return function(req,res,next)
    {
        console.log(req);
        next();
    }
}

function secondfunction(req,res,next)
{
    res.send("go there");
}



app.listen(3000);

您正在传递firstfunction但没有调用它。 你需要做

app.get("/users",firstfunction(),secondfunction);

暂无
暂无

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

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