I develop an Angular 1.6 single page application which is served by a static express server.
The Angular application should send async requests to a remote server over which I don't have control.
Unfortunately this remote server doesn't answer Angular preflight OPTIONS requests correctly - thus Angular denies to send the actual request as CORS is not fully supported by remote. (remote server is a Jira server instance and the issue is known but still not solved).
So I decided to try to proxy the requests to the remote server and making sure that Angular only communicates with the proxy-server which is sending requests to remote and returns remote server's response and making it fully CORS compatible.
In short:
Call Node server -> Angular request -> Node Http Proxy -> Remote Server
Remote server response -> Node Http Proxy (enriching CORS) -> Angular
This is what I got so far but as I am absolutely no node / express pro I don't see why I am getting no answer - I even don't know how to correctly debug this.
I commented the code in a way as I understand what should be going on.
const express = require('express');
const httpProxy = require('http-proxy');
const http = require("http");
const path = require("path");
const app = express();
const bodyParser = require('body-parser');
var proxyOptions = {
changeOrigin: true
};
httpProxy.prototype.onError = function (err) {
console.log(err);
};
// this should create the node http proxy server on port 3001
var apiProxy = httpProxy.createProxyServer(proxyOptions).listen(3001);
// I can see the following output when deploying my app
console.log('Forwarding API requests to ' + apiForwardingUrl);
// all incoming / requests on node server should be forwarded to Angular single page app's index.html
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
// providing static files for Angular app
app.use("/static", express.static(path.join(__dirname, "static")));
// all incoming requests to /jira/* should be forwared and responded by node http proxy server
app.all("/jira/*", function(req, res) {
apiProxy.web(req, res, {target: 'www.example.com'});
});
// make sure POST requests to node http proxy server are fully supported
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Create node server on port 3000
http.createServer(app).listen(3000);