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

标签 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("/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);
}

最好。

关于javascript - 在js文件中调用Express.js的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27419014/

相关文章:

javascript - 从node.js中的不同文件设置环境变量?

javascript - 在 toObject() 函数中使用 getter

javascript - ajax 调用另一个 url 重置 JsessionId 因此无法继续处理其他链接

javascript - 如何将字符串转换为html

Node.js puppeteer - 获取所需值不起作用(td :nth-child(n)

javascript - req.body.data 在expressjs中未定义

javascript - 在 div 内显示 JSON 内容

node.js - Heroku ENOENT : no such file or directory due to faulty express. js 路由

javascript - Node 应用程序未在远程服务器上运行,而是在 PC 上运行

javascript - 使用 Mongoose 时遇到问题 'Populate'