20

私はこのようなことをしようとしています:

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

以前はこれを使用していました:

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

基本的に、私はまだエクスプレスを使いたいと思っています...しかし、人々が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'));
});
4

4 に答える 4

44

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' });
});
于 2014-03-07T07:30:37.873 に答える
4

私はこれを機能させました。

  • Ghostをインストールし、それが機能していることを確認します (デフォルトのポートは 2368 です)。
  • Express を使用してノード Web アプリを作成します (ポート 80 でリッスンします) - ここでは特に何もしません
  • Web アプリにnode-http-proxy をインストールするnpm install http-proxy
  • 要求を Ghost サービスにプロキシする /blog* のワイルドカード ルートを作成する

    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  
      });
    });
    
  • サブディレクトリを使用するように 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',
    ...
    
  • これで、http: //yoursite.com/blogにアクセスできるようになり、すべてのルートが機能するはずです。

于 2014-01-15T23:32:24.073 に答える