var http = require('http'),
httpProxy = require('http-proxy'),
HttpProxyRules = require('http-proxy-rules');
// Set up proxy rules instance
var proxyRules = new HttpProxyRules({
rules: {
'.*/api': 'http://localhost:10002/api', // Rule (1)
'.*/': 'http://localhost:10003/' // Rule (2)
},
default: 'http://localhost:10003' // default target
});
// Create reverse proxy instance
var proxy = httpProxy.createProxy();
// Create http server that leverages reverse proxy instance
// and proxy rules to proxy requests to different targets
http.createServer(function(req, res) {
// a match method is exposed on the proxy rules instance
// to test a request to see if it matches against one of the specified rules
console.log(req.url);
var target = proxyRules.match(req);
if (target) {
return proxy.web(req, res, {
target: target
});
}
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('The request url and path did not match any of the listed rules!');
}).listen(process.env.PROXYPORT || 10001);

如您所见,我从https://www.npmjs.com/package/http-proxy-rules
复制了完全相同的代码问题:
当使用像http://host:10003这样的实际端口访问角度js app时,我可以看到图像。但是,当我使用代理端口http://host:10001,它将我带到端口10003时,除静态图像,css等外,其他一切都有效。
请为此提供任何帮助。
答案 0 :(得分:0)
这是一个愚蠢的错误,如果其他人遇到同样的问题,这里是解决方案,从过滤器中移除。*:
// Set up proxy rules instance
var proxyRules = new HttpProxyRules({
rules: {
'/api': 'http://localhost:10002/api', // Rule (1)
'/': 'http://localhost:10003/' // Rule (2)
},
default: 'http://localhost:10003' // default target
});