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?
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.
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?
Regards
Vidheer.
P.S: Please accept the answer if it address your query 🙂
User | Count |
---|---|
2 | |
1 | |
1 | |
1 | |
1 |