Node.js改变了http-proxy请求的路径名

时间:2013-01-18 02:51:51

标签: node.js http-proxy

我正在使用RoutingProxy类(在http-proxy包中找到)来代理我的节点应用程序中的某些请求。但是,我在向目标路径添加前缀时遇到了困难。例如,我正在尝试将http://localhost:8080/stylesheets/main.css代理为http://172.30.6.11:51161/mysite/stylesheets/main.css

这是我正在尝试做的一个愚蠢的例子:

// controllers/proxy.js
var httpProxy = require('http-proxy');

exports.request = function(options){
  var proxy = new httpProxy.RoutingProxy();

  return function(req, res){
    req.url = '/mysite' + req.url;
    proxy.proxyRequest(req, res, options);
  };
};


// app.js
// ...
var controllers = require('./controllers');
app.use(controllers.proxy.request({
  target: {
    host: '172.30.6.11',
    port: 55161
  }
});
// ...

不幸的是,在调用目标时永远不会添加前缀。有没有人知道如何才能实现这一目标?

1 个答案:

答案 0 :(得分:0)

通常我们不会将http-proxyexpress一起使用,而是http.createServer。但是,以下是this issue

的评论中的解决方法