繁体   English   中英

在js文件中调用Express.js的函数

[英]Calling a function of Express.js in a js file

我在Express.js中有一个函数,使用Node.js:

app.post("/checkExistsSpecific", function (req, res) {
    // do some code
}

我有另一个功能

app.post("/checkExistsGeneral", function (req, res) {
    // do some code
    // In this stage, I want to call /checkExistsSpecific API call
}

有没有什么方法来调用app.post("/checkExistsSpecific"..)app.post("/checkExistsGeneral"..)而不使用HTTP调用?

如果你只是想以正常方式调用函数:

function beingCalled (req, res) {

}


app.post("/checkExistsSpecific", beingCalled );


app.post("/checkExistsGeneral", function (req, res) {
   beingCalled (req,res);

}

要么

response.redirect("/checkExistsSpecific"..)正在寻找(可能是)。

这会将您的http调用重定向到checkExistsSpecific路由

为了做到这一点,我认为你应该使用命名函数作为你的POST回调而不是你现在拥有的匿名。 这样您就可以从任何需要的地方引用它们。

就像是:

function checkExistsSpecific(req, res){
    // do some code
}

app.post("/checkExistsSpecific", checkExistsSpecific);

app.post("/checkExistsGeneral", function (req, res) {
    // do some code
    // In this stage, I want to call /checkExistsSpecific API call

    checkExistsSpecific(req, res);
}

最好。

暂无
暂无

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

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