简体   繁体   English

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

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

I have aa function in Express.js, using Node.js: 我在Express.js中有一个函数,使用Node.js:

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

I have another function 我有另一个功能

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

Is there any way to call app.post("/checkExistsSpecific"..) from app.post("/checkExistsGeneral"..) without using HTTP call? 有没有什么方法来调用app.post("/checkExistsSpecific"..)app.post("/checkExistsGeneral"..)而不使用HTTP调用?

if you just want the function to be called in a normal way: 如果你只是想以正常方式调用函数:

function beingCalled (req, res) {

}


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


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

}

Or 要么

response.redirect("/checkExistsSpecific"..) is what you are looking for(might be). response.redirect("/checkExistsSpecific"..)正在寻找(可能是)。

this will redirect your http call to the checkExistsSpecific route 这会将您的http调用重定向到checkExistsSpecific路由

in order to do that, I think you should use named functions as your POST callbacks instead of anonymous as you currently have. 为了做到这一点,我认为你应该使用命名函数作为你的POST回调而不是你现在拥有的匿名。 This way you can refer to them from wherever you need to. 这样您就可以从任何需要的地方引用它们。

Something like: 就像是:

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);
}

Best. 最好。

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

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