proxy to support my Angular requests to a remote non

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);

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});
});
链接地址: http://www.djcxy.com/p/89114.html

上一篇: 以编程方式检查网站重定向

下一篇: 代理来支持我的Angular请求到远程非