Hi,
I am running my node app on APIGEE edge. Code is basicaaly fetching the contents from file in S3 bucket and converting the data to JSON format.
Code snippet:
index.js
var aws = require('aws-sdk');
var csv = require('csvtojson');
var http = require('http');
var apigee = require('apigee-access');
//aws.config.loadFromPath('./config.json');
aws.config.update({
accessKeyId: "key1",
secretAccessKey: "key2",
region: "us-east-1"
});
var s3 = new aws.S3({ apiVersion: '2006-03-01' });
// Get the object from the event and show its content type
var bucket = "topup-file-reader";
console.log("bucket = "+bucket);
var key = "file.csv";
console.log("key = "+key);
var jsonOutput;
var params = {
Bucket: bucket,
Key: key,
};
s3.getObject(params, (err, data) => {
if (err) {
console.log(err);
var message = `Error getting object ${key} from bucket ${bucket}. Make sure they exist and your bucket is in the same region as this function.`;
console.log(message);
// callback(message);
} else {
console.log('CONTENT TYPE:', data.ContentType);
console.log("data = "+data.Body.toString("utf-8"));
// callback(null, data.ContentType);
//read from file
var output = data.Body.toString("utf-8");
//converting code to json
var Converter = csv.Converter;
var converter = new Converter({});
converter.fromString(output,function(err,result){
console.log(JSON.stringify(result));
jsonOutput = result;
});
}
});
http.createServer(function (request, response) {
apigee.setVariable(request, "jsonOutput", jsonOutput);
response.writeHead(200, {'Content-Type': 'application/JSON'});
//response.end('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
package.json
{
"name": "csvreader",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"apigee-access": "^1.3.0",
"aws-sdk": "^2.9.0",
"csvtojson": "^1.1.3",
"http": "0.0.0"
}
}
I have added dependencies in my apigee edge using node management module. Dependencies are added with zip modules.
Code is working fine on my local machine but getting error on apigee
{
"fault": {
"faultstring": "Script node executed prematurely: syntax error\nsyntax error\n at module.js:439\n at module.js:474\n at module.js:356\n at module.js:312\n at module.js:497\n at startup (trireme.js:142)\n at trireme.js:923\n",
"detail": {
"errorcode": "scripts.node.runtime.ScriptExitedError"
}
}
}
Please help in identifying the error
Regards,
Ankita Jain