How to call an api from another api in expressjs?

眉间皱痕 提交于 2021-02-08 15:22:27

问题


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"


回答1:


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
}



回答2:


Create a common middleware which need to executed for both the routes.

Below is the code snippet for the same:

app.get('/test', test);
app.get('/check', check, test);

check and test are the middlewares which is used in common.




回答3:


To "call an API from another API", a quick and easy way is sending HTTP request inside Express server, browser would never know an internal HTTP invocation happens, not mention page-redirect. The benefit of this design includes:

  1. There's no need to change the current API design.
  2. The API invocation can be made exact like sent from browser.

Here is an example:

var http = require('http');

router.get('/test', function(req, res) {
  res.end('data_from_test');
});

router.get('/check', function(req, res) {
  var request = http.request({
    host: 'localhost',
    port: 3000,
    path: '/test',
    method: 'GET',
    headers: {
      // headers such as "Cookie" can be extracted from req object and sent to /test
    }
  }, function(response) {
    var data = '';
    response.setEncoding('utf8');
    response.on('data', (chunk) => {
      data += chunk;
    });
    response.on('end', () => {
      res.end('check result: ' + data);
    });
  });
  request.end();
});

The result of GET /check would be:

check result: data_from_test



回答4:


first define the /test handling function separately.
then you have two options.

// ========( 1 )====== testHandler as another function =============
// you can call this function where ever you want.
var testHandler = function(req, res){
    //do something
}
app.get('/test', testHandler);

app.get('/check', function(req, res){
  // you can call testHandler function here
  testHandler(req, res);
});

// ========( 2 )======= testHandler as a middleware =================
// if you want to call testHandler before running check handler function.
// 
var testHandler = function(req, res, next){
    //do something
    ...

    next();
}
app.get('/test', testHandler, function(req, res){});

app.get('/check', testHandler, function(req, res){
  // you can call testHandler function here
  testHandler(req, res);
});



回答5:


I used the request module inside a expressjs route

const request = require('request');

router.get('/test', function(req, res, next) {
    res.send('data_from_test');
});

router.get('/check', function(req, res, next) {
    request.get('http://localhost:3000/test');
});

Worked better than the http.request module



来源:https://stackoverflow.com/questions/44903053/how-to-call-an-api-from-another-api-in-expressjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!