1

I have a problem statement but I am unable to figure it out. The problem is, I have 2 different Links lets say A and B. And I make an API called C.
API C includes A and B both Links in it. I want to create a functionality in API C that if I call API C first time it should only execute Link A and if I call it second time it should be Link B and third time Link A and forth time Link B .... like a round robin algorithm.

var link1 = "http://link1";
var link2 = "http://link2";

exports.newApi = asyncHandler(async (req, res, next) => {
//If i call first time it should execute link1,
var data = link1;
res.status().json({success: true, data});

//On second call it should execute link2
var data = link2;
res.status().json({success: true, data}); 
})
Qasim
  • 33
  • 6
  • how are you tracking the request sessions ? Is it per request or per session? You could just have an array of your api's(enpoints for ex) and store the index of the last called. This seems like a strange problem though.. Some more info would be nice. [ask] – Pogrindis Sep 28 '21 at 12:31
  • More clarification/ code is needed. – Anatoly Sep 28 '21 at 13:20
  • alright, i try to add some code there. It may help to understand. – Qasim Sep 28 '21 at 14:47

1 Answers1

0

Create a middleware that handles all requests and count/toggle which API call you want

see this Q/A for example Get the number of Requests in Express

  • lets say if i add link3 and link4. and want to call different links on every request. for example on first request, it will call link1, second request link2, third request link3, forth request link4 and repeat. – Qasim Sep 28 '21 at 22:05