Sonal, it seems the “request” module is not found. You have not correctly structured the nodejs code so that it can be imported & deployed successfully. Often this means a dependency is missing from package.json .
ps: If you want to use “request”, you will need to use an older version. I suggest:
"request": "^2.67.0"
Are you saying we can call another flow or proxy on that server?
No, I’m saying you could populate the cache from within the nodejs logic. It probably looks something like this:
// sonal.js
var apigee = require('apigee-access');
var cache = apigee.getCache(undefined, {scope: 'application'}); // the default Apigee Edge cache ("un-named")
var requestjs = require('request');
var async = require('async');
var app = require('express')();
var bodyParser = require('body-parser');
var cacheItemTtl = 600;
var data = {
"msg" : [
{
"id" : 1234,
"body" : "Msg3"
},
{
"id" : 5678,
"body" : "Msg2"
},
{
"id" : 3345,
"body" : "Msg4"
}
]
};
// ================================================================
// Server interface
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
function getOne(options) {
return function(item, cb) {
options.uri = "https://foo.bar.bam/" + item.id;
requestjs(options, function(e, httpResp, body) {
if (e) { return cb(e); }
var cacheKey = 'element-' + item.id;
cache.put(cacheKey, body, cacheItemTtl, function(e) {
cb(e, "ok");
});
});
};
}
app.get('/foo', function(request, response) {
var options = {
timeout : 66000, // in ms
uri: "https://foo.bar.bam/", // will be replaced in the loop
method: 'get',
headers: {
accept : 'application/json'
}
};
async.mapSeries(data.msg, getOne(options), function(e, results) {
if (e) {
response.header('Content-Type', 'application/json')
.status(500)
.send(JSON.stringify({ error: e }, null, 2) + "\n");
return ;
}
response.header('Content-Type', 'application/json')
.status(200)
.send(JSON.stringify(results, null, 2) + "\n");
});
});
// default behavior
app.all(/^\/.*/, function(request, response) {
response.header('Content-Type', 'application/json')
.status(404)
.send('{ "message" : "This is not the server you\'re looking for." }\n');
});
var port = process.env.PORT || 5950;
app.listen(port, function() { });
The nodejs logic can use requestjs to invoke requests, and then can cache responses or anything it likes into the Apigee cache.