0

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);
LBA
  • 3,859
  • 2
  • 21
  • 60
  • You can use `console.log()` for debugging. – lin Nov 21 '17 at 12:30
  • The code seems right, it should work. I am not sure about the change origin part. Please provide us what exactly is the error you are facing. Is it in nodejs part, or the cors issue still exists. If the proxy works you should not have any cors problem because you request on the same domain. – Alex Michailidis Nov 21 '17 at 14:04
  • thanks - that was a tough one :-( at least for me. – LBA Nov 21 '17 at 15:03

1 Answers1

0

I guess I found the issue now:

When I look at 'targetURL' this is 'www.example.com' when I console.log it. But http-proxy seems to append (which is totally correct basically) the req.originalURL to it:

www.example.com/jira/somestuff // when I call /jira/somestuff from Angular

BUT: the /jira/ part of the URL just exists to differentiate between proxied and non-proxied calls.

So the right URL needs to be www.example.com/somestuff.

I tried several ways now to change the targetUrl, req.originalUrl etc. to fix this - so far without success.

So I successfully tested it with this ugly solution and my next issue is around SSL, but obviously he connected:

app.all("/jira/*", function(req, res) {
    req.originalUrl = req.originalUrl.slice(5);
    apiProxy.web(req, res, {target: apiForwardingUrl});
});
LBA
  • 3,859
  • 2
  • 21
  • 60