Orchestrating Multiple endpoints in node.js

Not applicable

hi

I have requirements to orchestrate between 3 Rest endpoints as below using node.js.

1)response from 1st end point would return a array of Json values.(Response example: [{emp:1,empName:roger},{emp:2, empName: dany}]

2)For every array element, I need to extract specific values and make a call to 2nd end point.This endpoint would indicate if the resource value is valid or not. Req example: {emp:1}. Response:{valid: true}

3)Based on the response, I need to optionallyIif it is valid) make a call to 3 rd endpoint and enrich the details of original response and pass it to requestor. If it is not valid, no enrichment needed.Req. example: {{emp:1,empName:roger} EndpointResponse: {emp:1,empName:roger, empCity:NJ}

Final Response to requestor:[{emp:1,empName:roger, empCity:NJ},{emp:2, empName: dany}]

I tried to explore asynch modules but not sure if the individual array could be split and consolidated as per above. Please advice on how to achieve this.

Solved Solved
1 1 2,538
1 ACCEPTED SOLUTION

A nodejs app that runs within Apigee Edge works just the same as a nodejs app that runs outside of Apigee Edge. Therefore I suggest you test the code outside of Apigee Edge before deploying it into Edge.

You may want to try the async module . There is a async.map function that does what you want.

Code example:

var http = require('http');
const PORT=8080;
var clientRequest = require('request');
var async = require('async');


var target1 = 'http://....';
var target2 = 'http://...';

function invokeOne(item, cb) {
  clientRequest(target2 + '/' + item.empName, function (error, resp, body) {
    if (error) {
      console.log('error: ' + error);
    }
    console.log('body: ' + body);
    // invoke callback to signal the invocation is complete
    cb(null, JSON.parse(body));
  });
}
   

function handleRequest(request, response) {
  clientRequest(target1, function (error, response2, body) {
    if (error) {
      response.setHeader('Content-Type', 'application/json');
      response.statusCode = 500;
      response.end(JSON.stringify({ error: error}));
      return;
    }
    
    if (response2.statusCode != 200) {
      response.setHeader('Content-Type', 'application/json');
      response.statusCode = response2.statusCode;
      response.end(JSON.stringify({ error: 'bad status'}));
      return;
    }
      
    body = JSON.parse(body);
    async.map(body, invokeOne, function(error, results) {
      // ....
      // results is an array of all results posted by invokeOne
      response.setHeader('Content-Type', 'application/json');
      response.end(JSON.stringify(results));
    });
    
  });
}

var server = http.createServer(handleRequest);

server.listen(PORT, function(){
    console.log("Server listening on: http://localhost:%s", PORT);
});

View solution in original post

1 REPLY 1

A nodejs app that runs within Apigee Edge works just the same as a nodejs app that runs outside of Apigee Edge. Therefore I suggest you test the code outside of Apigee Edge before deploying it into Edge.

You may want to try the async module . There is a async.map function that does what you want.

Code example:

var http = require('http');
const PORT=8080;
var clientRequest = require('request');
var async = require('async');


var target1 = 'http://....';
var target2 = 'http://...';

function invokeOne(item, cb) {
  clientRequest(target2 + '/' + item.empName, function (error, resp, body) {
    if (error) {
      console.log('error: ' + error);
    }
    console.log('body: ' + body);
    // invoke callback to signal the invocation is complete
    cb(null, JSON.parse(body));
  });
}
   

function handleRequest(request, response) {
  clientRequest(target1, function (error, response2, body) {
    if (error) {
      response.setHeader('Content-Type', 'application/json');
      response.statusCode = 500;
      response.end(JSON.stringify({ error: error}));
      return;
    }
    
    if (response2.statusCode != 200) {
      response.setHeader('Content-Type', 'application/json');
      response.statusCode = response2.statusCode;
      response.end(JSON.stringify({ error: 'bad status'}));
      return;
    }
      
    body = JSON.parse(body);
    async.map(body, invokeOne, function(error, results) {
      // ....
      // results is an array of all results posted by invokeOne
      response.setHeader('Content-Type', 'application/json');
      response.end(JSON.stringify(results));
    });
    
  });
}

var server = http.createServer(handleRequest);

server.listen(PORT, function(){
    console.log("Server listening on: http://localhost:%s", PORT);
});