Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call an api from another api in expressjs?

I have an api like this:

app.get('/test', (req, res) => {
   console.log("this is test");
});

and another api:

app.get('/check', (req, res) => {
   //I want to call "test" api without redirect to it. 
});

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"

like image 886
Keitaro Urashima Avatar asked Jul 04 '17 10:07

Keitaro Urashima


People also ask

Can we call API within API?

This approach isn't limited to your API; you can use it to call any API. Your Express application needs to be able to call the API URLs that you set up in chapter 6—sending the correct request method, of course—and then be able to interpret the response. To help, you'll use a module called request .


1 Answers

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
}
like image 178
Chandra Eskay Avatar answered Oct 04 '22 09:10

Chandra Eskay