Cross-Origin-Resource-Sharing (CORS) on Fujitsu’s K5 Platform

2017-03-24

Cross-Origin-Resource-Sharing (CORS) on Fujitsu’s K5 Platform

Any developers out there that have moved their front-end app development from a production environment where you’re within the one domain e.g. https://allthingscloud.eu to a local ‘desktop’  where you’re now making requests from http://localhost will be familiar with this error:

It’s a security mechanism built into almost all modern web browsers designed to prevent malicious attacks such as cross-site-scripting.

The are two ways to address this challenge provided that you have a legitimate need to work across different domains:

Fujitsu’s K5 API endpoints do not support CORS at present so a CORS Proxy is what I opted for during my recent development challenge. This is a lot easier than it sounds especially if you like working with NodeJS. There are many NPM modules available that require only a single line or two of code to implement a CORS proxy server. For example corsproxy and corsproxy-https. However, after some testing I decided upon the cors-anywhere module as this module forwards both the http response body AND the response headers…something not all proxies bother to do.

The simple proxy server looks like this:

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

// Heroku defines the environment variable PORT, and requires the binding address to be 0.0.0.0
var host = process.env.PORT ? '0.0.0.0' : '127.0.0.1';
var port = process.env.PORT
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
originWhitelist: [], // Allow all origins
//requireHeader: ['origin', 'x-subject-token'],
}).listen(port, host, function() {
console.log('Running CORS Anywhere on ' + host + ':' + port);
});

view raw
cors_proxy_service.js
hosted with ❤ by GitHub

The Angular 2 application where I used it can be downloaded here – https://github.com/allthingsclowd/K5_Angular_2_Example_Login

Happy Stacking!

withk5youcan

Originally published on allthingscloud.eu (2017-03-24).

← All posts