2016-05-31 83 views
0

我正嘗試在Couchbase SyncGateway前構建一個反向代理。在向同步網關發送請求之前,我想將它們發送到身份驗證服務器進行身份驗證,如果一切正常,請將請求(未修改爲原始)發送到同步網關。數據庫沒有及時更新客戶端修改,我相信這是因爲我沒有成功代理PUT/POST請求。這裏是我有的代碼:節點js反向代理PUT/POST請求w/Couchbase同步網關

var http = require('http'); 
var httpProxy = require('http-proxy'); 
var apiProxy = httpProxy.createProxyServer(); 
var request = require('request').defaults({json: true}); 

var authServer = 'http://authserverdns:5000'; 
var syncGateway = 'http://syncgatewaydns:4984'; 

http.createServer(function (req, res) { 
    if (req.method == 'POST' || req.method == 'PUT') { 
     req.body = ''; 

     req.addListener('data', function(chunk) { 
      req.body += chunk; 
     }); 

     req.addListener('end', function() { 
      processRequest(req, res); 
     }); 
    } else { 
     processRequest(req, res); 
    } 

}).listen(8080); 

function processRequest(req, res) { 
    request(authServer, function(error, response, body) { 
     if (body.authenticated) { 
      console.log('authenticated !!!'); 
      apiProxy.web(this.req, this.res, {target: this.sg}); 
     } else { 
      console.log('request denied !!!'); 
     } 
    }.bind({req: req, res: res, sg: syncGateway})); 
} 

起初我使用的是快遞服務器,並且有同樣的問題。當我研究這個問題時,看起來可能是Express和代理PUT/POST請求存在問題。所以,我試圖在這裏使用一些例子,這就是我最終的結果,但仍然不起作用。關於我在哪裏出錯的任何想法?認證的印刷品,所以我知道我正在進入代理點。同步網關對GET請求似乎沒問題。

謝謝

回答

0

呃。我沒有將其餘的URL添加到Sync Gateway的轉發地址。 here幫助。