节点http-proxy和express

BRo*_*ers 19 node.js express node-http-proxy

我正在尝试做这样的事情:

// Setup prox to handle blog requests
httpProxy.createServer({
    hostnameOnly: true,
    router: {
        'http://localhost': '8080',
        'http://localhost/blog': '2368' 
    }
}).listen(8000);
Run Code Online (Sandbox Code Playgroud)

以前我用过这个:

http.createServer(app).listen(app.get('port'), function(){
    console.log("Express server listening on port " + app.get('port'));
});
Run Code Online (Sandbox Code Playgroud)

基本上,我想仍然使用快递...但是,当人们去http://localhost/blog博客但仍然被服务port 8080(最终将是80端口)

所以我把它切换到了这个并且效果更好.问题是快递接管了路由(从我能说的)

var options = {
    // pathnameOnly: true,
    router: {
        'localhost': 'localhost:8080',
        'localhost/blog': 'localhost:2368'
    }
}

// Setup prox to handle blog requests
var proxyServer = httpProxy.createServer(options);
proxyServer.listen(9000);

require('./app/server/router')(app);

http.createServer(app).listen(app.get('port'), function(){
    console.log("Express server listening on port " + app.get('port'));
});
Run Code Online (Sandbox Code Playgroud)

Cha*_*ler 38

使用带有express的http-proxy 1.0:

var httpProxy = require('http-proxy');

var apiProxy = httpProxy.createProxyServer();

app.get("/api/*", function(req, res){ 
  apiProxy.web(req, res, { target: 'http://google.com:80' });
});
Run Code Online (Sandbox Code Playgroud)

  • 在api文档中找到它,`app.all`,http://expressjs.com/api.html (9认同)
  • 如何将其推广到其他http动词,如post\put\delete? (3认同)

Sel*_*ish 8

一个非常简单的解决方案,无缝地工作,并使用cookie /身份验证,使用express-http-proxy:

var proxy = require('express-http-proxy');

var blogProxy = proxy('localhost/blog:2368', {
    forwardPath: function (req, res) {
        return require('url').parse(req.url).path;
    }
});
Run Code Online (Sandbox Code Playgroud)

然后简单地说:

app.use("/blog/*", blogProxy);
Run Code Online (Sandbox Code Playgroud)

我知道我参加这个派对已经很晚了,但我希望这对某人有所帮助.


Mic*_*ser 4

我得到了这个工作。

  • 安装 Ghost并确保其正常工作(默认端口为 2368)
  • 使用express(侦听端口80)创建您的节点Web应用程序 - 这里没什么特别的
  • 在您的 Web 应用程序中安装node-http-proxy npm install http-proxy
  • 为 /blog* 创建通配符路由,代理对 Ghost 服务的请求

    var httpProxy = require('http-proxy');
    
    var proxy = new httpProxy.RoutingProxy();
    app.get('/blog*', function (req, res, next) {
      proxy.proxyRequest(req, res ,{
        host: 'moserlap.splitvr.com',
        port: 2368  
      });
    });
    
    Run Code Online (Sandbox Code Playgroud)
  • 更新 Ghost 配置以使用子目录(仅在 0.4.0+ 中支持)

    config = {
      // ### Development **(default)**
      development: {
      // The url to use when providing links to the site, E.g. in RSS and email.
      url: 'http://127.0.0.1/blog',
    ...
    
    Run Code Online (Sandbox Code Playgroud)
  • 您现在应该能够访问http://yoursite.com/blog并且所有路由都有效。

  • 这个答案不适用于 http-proxy 1.0,httpProxy.RoutingProxy 不再是一种方法。查看我的答案以获取更新 (3认同)