【发布时间】:2018-05-09 20:51:47
【问题描述】:
- 我有一个使用 pm2 部署的节点 js 应用程序(端口 10002)和一个 angular-js(端口 10003)应用程序。当您使用冒号(:) 指定端口时,一切都很好,例如 host:10003
- 出于某种原因,我不得不使用 http-proxy 和 http-proxy-rules 创建一个 nodejs 代理,以将 10001 转发到 10002 或 10003,如下所示:
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 等实际端口访问 Angular js 应用程序时,我可以看到图像。但是当我使用代理端口 http://host:10001 时,它会将我带到端口 10003,除了静态图像、css 等之外,一切正常。
感谢您对此的任何帮助。
【问题讨论】:
标签: node.js static http-status-code-404 http-proxy