What I am trying to do:
代理一个运行在 https://127.0.0.1:443/api/
上的java api,我的UI运行在非SSL http://127.0.0.1:1337/上,以便绕过一些CORS问题 .
My attempt:
-
将SSL端口443上的api代理到我的非SSL开发端口1338 .
-
将我的UI代理到1337
-
代理1137到
:8080/index.html
和代理1338到:8080/api/
-
从localhost:8080访问我的应用程序
My problem:
用户界面很好......但我无法在 :8080/api/httpSession/init
点击API
是的,我仍然可以在 https://localhost/api/httpSession/init
点击API
api.js - 将index.html渲染为:1337
var app = express();
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
var options = {
changeOrigin: true,
target: {
https: true
}
};
httpProxy.createServer(443, '127.0.0.1', options).listen(1338);
start.js - 代理1337和1338到8080
// First I start my two servers
uiServer.start(); // renders index.html at 1337
apiServer.start(); //
// I attempt to patch them back into one single non-SSL port.
app
.use('/', proxy({target: 'http://localhost:1337/'}))
.all('/api/*', proxy({target: 'http://localhost:1338/'}))
.listen(8080, function () {
console.log('PROXY SERVER listening at http://localhost:%s', 8080);
});
2 回答
你要找的是request piping . 试试这个例子:
如果无法访问api,请确保添加一些错误处理,例如this SO solution .
对于代理问题,我的猜测是它将
/api/*
保留在URL中并且在API服务中的路由器上不存在 . 您可以尝试将/api
添加到API服务中的路由器,因为它会在发送时保持url字符串相同 . 否则,您可能需要代理并重写URL,以便API将请求与路由匹配 .另一方面,如何安装
cors
模块并在应用程序中使用?我做了类似的事情,并且在没有所有代理项目的情况下运行良好 . https://www.npmjs.com/package/cors