In order to access an external API that does not support CORS for my front-end (angular) application, I have implemented a simple proxy using Node.JS / Express.JS to handle the requests. This setup allows me to securely store my api credentials at the proxy level, preventing any potential misuse by users on the front end.
Everything is functioning flawlessly as intended.
Check out the code snippet below:
var request = require('request');
var config = require('./config');
var url = config.api.endpoint;
var uname = config.api.uname;
var pword = config.api.pword;
var headers = {
"Authorization" : 'Basic ' + new Buffer(uname + ':' + pword).toString('base64'),
"Accept" : "application/json"
};
exports.get = function(req, res) {
var api_url = url+req.url;
var r = request({url: api_url, headers: headers});
req.pipe(r).pipe(res);
};
The API endpoint I am interacting with only provides XML responses. To convert these responses into JSON on the client side, I utilize xml2js library within my front-end application.
Although this setup works efficiently, I am considering offloading the XML to JSON conversion process from the client to the server to improve performance.
I believe creating something like the following structure would streamline this process:
req.pipe(r).pipe(<convert_xml_to_json>).pipe(res);
However, implementing this functionality is uncharted territory for me.
In essence, my goal is to construct an XML to JSON proxy layer on top of an existing API service.
Despite numerous resources available on Stack Overflow covering topics like "creating a proxy" and "XML to JSON conversion", I have yet to find a comprehensive guide that combines both aspects seamlessly.