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

时间:2014-12-11 09:09:56

标签: javascript node.js express

我在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("/checkExistsSpecific"..)拨打app.post("/checkExistsGeneral"..)

2 个答案:

答案 0 :(得分:2)

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

function beingCalled (req, res) {

}


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


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

}

或者

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

这会将您的http呼叫重定向到checkExistsSpecific路由

答案 1 :(得分:1)

为了做到这一点,我认为你应该使用命名函数作为你的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);
}

最佳。