I am able to read contents of a file locally in Apigee using nodejs. Could anyone share how to read a file from a repository using Apigee Node JS proxy ?
I am using below code but getting an error as below though the file is present and able to curl.
Error: ENOENT: http://<link>/abc.json
'use strict'; const fs = require('fs'); var http = require('http'); var url = require('url'); var path = require("path"); var fieldvalues = {}; http.createServer(function (req, res) { var pathname = "http://<link>/abc.json"; console.log("pathname =" +pathname); // read file from file system fs.readFile(pathname, function(err, data){ if(err){ //res.statusCode = 500; console.log("Error is "+err); //res.end("Error getting the file: ${err}"); } else { // if the file is found, set Content-type and send data res.setHeader('Content-type', 'application/json' ); // res.end(data); //res.end(JSON.stringify(data, null, 2) + '\n'); var filedata= JSON.parse(data); res.end(filedata); console.log(JSON.stringify(filedata, null, 2) + '\n'); } }) // }); }).listen(parseInt(9000)); console.log("Server listening on port 9000");
You cannot use fs.readFile() for reading files from remote HTTP endpoints. To read from an http endpoint, use the request module.
var request=require('request'); // v2.71.0 when used within Apigee var url = "https://example.com/foo/bar"; request({url:url}, function(error, response, body) { if (error) { console.log('error:', error); } var fileData = JSON.parse(body); ... });
The latest version of request that you can use within Apigee Edge is 2.71.0.
update your dependencies in package.json like so:
"request":"2.71.0"
For more, see here: https://community.apigee.com/answers/51554/view.html
@Dino - I was able to install the request module in trial account and invoke the remote file.However,unable to install request module in licensed Org account using the link
I am getting a 401 Unauthorised error though the credentials are correct.Is it not allowed for licensed Org accounts to install the npm modules ?