I am trying to schedule/poll an proxy using javascript policy.

Scenario : There is a server which hosts its data every hour through an API.

From the below link I came to know that " the nodejs code that you embed into an Apigee Edge proxy starts running as soon as you deploy your api proxy."

https://community.apigee.com/questions/27019/schedule-a-thread-in-apigee-edge.html

So I wrote a code is javascript using cron , below is the code.

//This code will hit http://mocktarget.apigee.net every 4th second.

var schedule = require('node-schedule');

var j = schedule.scheduleJob('*/4 * * * * *', function(){

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; var xhr = new XMLHttpRequest(); xhr.open('GET',"http://mocktarget.apigee.net",false); xhr.send(); console.log(xhr.responseText); console.log(xhr.status);

});

As soon as I ran this code I got this

Execution of cron failed with error: Javascript runtime error: "ReferenceError: "require" is not defined. (cron:1)"

To resolve this I imported require.js in jsc and included like this

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="cron">

<DisplayName>cron</DisplayName>

<Properties/>

<ResourceURL>jsc://cron</ResourceURL>

<IncludeURL>jsc://require.js</IncludeURL>

</Javascript>

Now the above error is resolve , new error is

Execution of cron failed with error: Exception thrown from JavaScript : Error: Module name "node-schedule" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded (cron#143)

How to import this node-schedule module now?

0 3 511
3 REPLIES 3

@Divakar Sharma,

Refer to Including multiple javascript dependencies for steps on how to add multiple dependencies or external modules to be used by Javascript.

If you want to import NodeJS module, then this How to import a NodeJS module to apigee edge? would be helpful.

@AMAR DEVEGOWDA

That's how I included and resolved the require error that I was getting previously.

But Its little different with "node-schedule" because as soon as I include Schedule.js to resolve this issue it gives me

Execution of cron failed with error: Exception thrown from JavaScript : Error: Module name "events" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded (cron#143)

because "events" is mentioned in the schedule.js file ,see below


var events = require('events'),

util = require('util'),

cronParser = require('cron-parser'),

CronDate = require('cron-parser/lib/date'),

lt = require('long-timeout'), sorted = require('sorted-array-functions');

I included the event.js file same way as i included "require","schedule"

<IncludeURL>jsc://require.js</IncludeURL> <IncludeURL>jsc://schedule.js</IncludeURL> <IncludeURL>jsc://events.js</IncludeURL>



but it is still giving me

Execution of cron failed with error: Exception thrown from JavaScript : Error: Module name "events" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded (cron#143)

can in include package.json or a zip on module in jsc?

Hi @Divakar Sharma,

Can you try including require node modules using management API and see if its able to resolve?

https://apidocs.apigee.com/management/apis/post/organizations/%7Borg_name%7D/apis/%7Bapi_name%7D/rev...

Regards

Vidheer.

P.S: Please accept the answer if it address your query 🙂