在js文件中调用Express.js的函数
我在Express.js中有一个函数,使用Node.js:
app.post("/checkExistsSpecific", function (req, res) { // do some code }
我有另一个function
app.post("/checkExistsGeneral", function (req, res) { // do some code // In this stage, I want to call /checkExistsSpecific API call }
有没有办法从app.post("/checkExistsGeneral"..)
调用app.post("/checkExistsGeneral"..)
而不使用HTTP调用?
为了做到这一点,我认为你应该使用命名函数作为你的POSTcallback,而不是你现在拥有的匿名。 这样,你可以从你需要的地方引用它们。
就像是:
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调用redirect到checkExistsSpecific
路由