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



我在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
}

有没有办法在不使用 HTTP 调用的情况下从app.post("/checkExistsGeneral"..)调用app.post("/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);
}

最好。

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

function beingCalled (req, res) {
}

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

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

response.redirect("/checkExistsSpecific"..)是您要找的(可能是)。

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

最新更新