NodeJs -Module implementation in Apigee

Not applicable

Hi,

I have to make 7 service calls in parallel & mashup the data . So I went with NodeJS approach & implemented this requirement.

I am new to nodejs and have few queries..

Query1:

I already written lot of Utlitily function using pure JS and currently using them in my policy flows.

How to reuse those JS utility functions inside my Node JS code?

Example::

Scripts/jsc/commonutil.js

function1(){

};
function2(){

};
Scripts/node/

servicecallout.js - I want to call commonutil.js (function1,function2...) inside node.js file . is it feasible to do?

I am aware of Modules concept in nodejs.. I dont want to duplicate commonUtil inside Node folder , so I want to make use of jsc/commonUtil,js inside my NodeJs file

Query 2:

in Proxy Request Preflow and Request flow.. I already set bunch of flow variables using Context.SetVariable('XX') in the proxy request flow

How to retrieve those apigee flow variables & context flow variables inside my node.js file??

Regards

0 3 228
3 REPLIES 3

Not applicable

Hi @Murali. All good questions!

For Query 1: I understand the need to DRY (Don't Repeat Yourself) from jsc folder and code for Node.js app. However, do you really need to keep it in both places? If not, I'd recommend writing what you already mentioned. A small module commonutil.js and expose each one of those functions as exports. e.g.

function1() {
  ...
}
function2() {
  ...
}

module.exports = {  
  function1 : function1,
  function2 : function2
} 

If you really need to keep the code in both places synchronized with little intervention, I'd recommend leveraging build tools for writing the code in jsc folder and also generate the commonutil.js for you. Writing one of these modules is very easy with Apigee Grunt. I have already written a bunch of plugins and tutorials for it, so you don't to start from scratch. Take a look at Tasks folder, which contains a bunch of tasks to help you get started with.

For Query 2: For getting access to variables, take a look at apigee-access NPM module. For request and query parameters. Since, Node.js acts as a target you can leverage the same handlers from Express.js to get access to headers and query params.

Hope it helps! Please let me know your thoughts here.

I think, I am good with query1.

Query 2 :: I am still not clear how to get the user defined variables in Node.js

for example :

in one of the JS policy i set this variable -

context.setVariable('incoming-request-data', "hello");

I want to access this inside node.js code

var 'incoming-request-data=context.getVariable('incoming-request-data');

Apigee-access says.. i can only able to access request object that came from the http module

Please advice.. how to overcome this challege

Thanks Diego