[英]Node Proxy - Proxy a SSL localhost target from a basic http server
What I am trying to do: 我想做什么:
Proxy a java api that runs on https://127.0.0.1:443/api/
along side my UI that runs on non-SSL http://127.0.0.1:1337/ in order to circumnavigate some CORS issues. 代理一个运行在https://127.0.0.1:443/api/
上的java api,我的UI运行在非SSL http://127.0.0.1:1337/上 ,以便绕过一些CORS问题。
My attempt: 我的尝试:
:8080/index.html
and proxy 1338 to :8080/api/
代理1137到:8080/index.html
和代理1338到:8080/api/
My problem: 我的问题:
The UI comes in just fine... but I can not hit the API at :8080/api/httpSession/init
用户界面很好......但我无法点击API :8080/api/httpSession/init
Yes, I can still hit the API at https://localhost/api/httpSession/init
是的,我仍然可以通过https://localhost/api/httpSession/init
api.js - Renders index.html at :1337 api.js - Renders index.html at: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 - Proxies 1337 and 1338 into 8080 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);
});
What you're looking for is request piping . 您正在寻找的是请求管道 。 Try this example: 试试这个例子:
// Make sure request is in your package.json
// if not, npm install --save request
var request = require('request');
// Intercept all routes to /api/...
app.all('/api/*', function (req, res) {
// Get the original url, it's a fully qualified path
var apiPath = req.originalUrl;
// Form the proxied URL to your java API
var url = 'https://127.0.0.1' + apiPath;
// Fire off the request, and pipe the response
// to the res handler
request.get(url).pipe(res);
});
Make sure to add some error handling if the api can't be reached, such as this SO solution . 如果无法达到api,请确保添加一些错误处理,例如此SO解决方案 。
For the proxy issue, my guess is that it is keeping the /api/*
in the url and that's not present on the router in your API service. 对于代理问题,我的猜测是它将/api/*
保留在url中并且在API服务中的路由器上不存在。 You could try adding /api
to the router in the API service since it's going to keep the url string the same when it sends it. 您可以尝试将/api
添加到API服务中的路由器,因为它会在发送时保持url字符串相同。 Otherwise, you likely need to proxy and rewrite the url so that the API will match the request to a route. 否则,您可能需要代理并重写URL,以便API将请求与路由匹配。
On another note, what about just installing the cors
module and using in the app? 另一方面,如何安装cors
模块并在应用程序中使用呢? I do something similar and it's working well without all the proxy items. 我做了类似的事情,并且在没有所有代理项目的情况下运行良好。 https://www.npmjs.com/package/cors https://www.npmjs.com/package/cors
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.