Any plans to support nodejs 'require' statement for sharedflows?

Any plans to support nodejs 'require' statement for sharedflows?

Here is an example of the type of error that I saw:

action CONTINUE ABORT enforcement request request internal false false javascript-errorMessage Execution of JS_SetKeys_KVM failed with error: Javascript runtime error: "ReferenceError: "require" is not defined. (JS_setKeys_KVM_read.js:2)" javascript-executionTime 3 javascript-name JS_setKeys_KVM_read.js javascript-stackTrace ReferenceError: "require" is not defined. at JS_setKeys_KVM_read_js:2:0

I was trying to refactor some code to get it to be more testable but alas looks like it's not an option currently with the type of nodejs that Apigee supports currently.

You probably already know this but 'require' is very common in nodejs - https://medium.freecodecamp.org/requiring-modules-in-node-js-everything-you-need-to-know-e7fbd119be8

Thanks.

1 2 322
2 REPLIES 2

Hmm, i'm sorry about the confusion. There are multiple way you can use JavaScript within an API Proxy.

  1. as the logic for a JavaScript callout
  2. as the logic for a Nodejs Target
  3. as the logic for a Hosted Target

They all use JavaScript, but each of these runs in a different JS environment.

The first two use the Rhino runtime. This is a Java implementation of JavaScript. It supports ES2015, partially. You can see the test results here: http://mozilla.github.io/rhino/compat/engines.html . The callout does not use nodejs.

The Hosted Target can use whatever node runtime you like. It will use bonafide node, running on v8.

The latter two - node target and hosted target - will support the require statement.

The JavaScript callout does not support the require statement. (But you can use "browserify" to package your code that uses 'require' into a bundle that can be used by a JS Callout. But you cannot depend on any modules that have platform-specific code, because it doesn't run in v8! )

I believe the Edge SaaS uses Rhino 1.7.9 (latest).

Here's a comparison matrix

option runtime nodejs? notes
JS Callout Rhino no No plans to support nodejs
Nodejs target Rhino yes Will be deprecated in favor of Hosted Targets.
Hosted target node (you choose) yes runs external to the MP

The "target" options apply only to API Proxies, not to SharedFlows. Therefore if you want to use JS in a sharedflow, you need to use the JS callout, which means you cannot use 'require'.

An alternative to sharedflows is to package your logic as an api proxy, and then use proxy chaining instead of a flowcallout. This would allow you to use a hosted target to run your JS Code.

I hope this is helpful .

Thanks for clarifying. Hmm, I was hoping to use the require statement in JavaScript / nodejs in the shared flow. The reason being is that way I can have testable functions. I was using Mocha and Chai for unit testing and running 'npm test'.

Thanks for sharing the other options. I might consider those as time allows. Cheers.