如何从ExpressJS中的另一个API调用一个API? [英] How to call an api from another api in expressjs?
本文介绍了如何从ExpressJS中的另一个API调用一个API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的api:
I have an api like this:
app.get('/test', (req, res) => {
console.log("this is test");
});
和另一个api:
app.get('/check', (req, res) => {
//I want to call "test" api without redirect to it.
});
我想在检查" api中调用测试" api,而无需重定向到测试" api,只需在测试" api中执行功能即可.上面是示例代码.因为我不想要将功能从测试" API重写为检查"
I want to call "test" api in "check" api without redirect to "test" api, just do the function in the "test" api. Above is the example code. 'Cause I dont' want to rewrite function from "test" api to "check"
推荐答案
简单的解决方案是定义可以同时使用两个请求路由调用的方法.
Simple solution is to define a method which can be called using both request routes.
app.get('/test', (req, res) => {
console.log("this is test");
callMeMayBe();
});
callMeMayBe()
{
//Your code here
}
这篇关于如何从ExpressJS中的另一个API调用一个API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文