简体   繁体   中英

Calling a function of Express.js in a js file

I have aa function in Express.js, using 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?

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).

this will redirect your http call to the checkExistsSpecific route

in order to do that, I think you should use named functions as your POST callbacks instead of anonymous as you currently have. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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