http-proxy-rules不提供图像&其他资源

时间:2018-05-09 20:51:48

标签: node.js static http-status-code-404 http-proxy

  1. 我有一个节点js app(端口10002)和一个使用pm2部署的angular-js(端口10003)应用程序。当您使用冒号(:)(如host:10003)
  2. 指定端口时,一切都很好
  3. 出于某种原因,我不得不使用http-proxy和http-proxy-rules创建一个nodejs代理,以便将10001转发到10002或10003,如下所示:
  4. 
    
    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等外,其他一切都有效。

    请为此提供任何帮助。

1 个答案:

答案 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
});