2013-02-27 22 views
6

Próbuję użyć węzła-http-proxy jako odwrotnego proxy, ale nie mogę uzyskać żądań POST i PUT do pracy. Plik server1.js jest odwrotnym proxy (przynajmniej dla żądań z adresem url "/ forward-this") a serwer server2.js jest serwerem, który odbiera proxy żądania. Wyjaśnij, co robię nieprawidłowo.Jak odwrócić żądania klienta POST i żądania PUT za pomocą węzła-http-proxy

Oto kod dla server1.js:

// File: server1.js 
// 

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

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

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

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

}).listen(8080); 

function processRequest(req, res, proxy) { 

    if (req.url == '/forward-this') { 
     console.log(req.method + ": " + req.url + "=> I'm going to forward this."); 

     proxy.proxyRequest(req, res, { 
      host: 'localhost', 
      port: 8855 
     }); 
    } else { 
     console.log(req.method + ": " + req.url + "=> I'm handling this."); 

     res.writeHead(200, { "Content-Type": "text/plain" }); 
     res.write("Server #1 responding to " + req.method + ": " + req.url + "\n"); 
     res.end(); 
    } 
} 

A oto kod dla server2.js:

// File: server2.js 
// 

var http = require('http'); 

http.createServer(function (req, res, proxy) { 
    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(8855); 

function processRequest(req, res) { 
    console.log(req.method + ": " + req.url + "=> I'm handling this."); 

    res.writeHead(200, { 'Content-Type': 'text/plain' }); 
    res.write("Server #2 responding to " + req.method + ': url=' + req.url + '\n'); 
    res.end(); 
} 

Odpowiedz

4

http-proxy zależy od data i end zdarzeń dla żądań POST/PUT . Opóźnienie między momentem, w którym serwer1 odbiera żądanie, a serwerem proxy, oznacza, że ​​serwer HTTP-proxy całkowicie je omija. Masz dwie opcje, aby to zadziałało poprawnie - możesz buffer the request lub użyć zamiast tego routing proxy. Proxy routingu wydaje się najbardziej odpowiednie, ponieważ potrzebujesz tylko proxy dla podzbioru żądań. Oto zmienione server1.js:

// File: server1.js 
// 

var http = require('http'); 
var httpProxy = require('http-proxy'); 
var proxy = new httpProxy.RoutingProxy(); 

http.createServer(function (req, res) { 
    if (req.url == '/forward-this') { 
     return proxy.proxyRequest(req, res, { 
      host: 'localhost', 
      port: 8855 
     }); 
    } 

    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) { 
    console.log(req.method + ": " + req.url + "=> I'm handling this."); 

    res.writeHead(200, { "Content-Type": "text/plain" }); 
    res.write("Server #1 responding to " + req.method + ": " + req.url + "\n"); 
    res.end(); 
} 
+0

To działało idealnie. Dzięki! –

+0

Mimo że jest to stara odpowiedź, aby to działało, powinieneś zamienić 'var proxy = new httpProxy.RoutingProxy();' z 'var proxy = httpProxy.createProxyServer ({});' – Saber

1

Oto moje rozwiązanie do proxy żądań POST. To nie jest idealne rozwiązanie, ale działa i jest łatwe do zrozumienia.

var request = require('request'); 

var http = require('http'), 
    httpProxy = require('http-proxy'), 
    proxy = httpProxy.createProxyServer({}); 

http.createServer(function(req, res) { 
    if (req.method == 'POST') { 
     request.post('http://localhost:10500/MyPostRoute', 
        {form: {}}, 
        function(err, response, body) { 
         if (! err && res.statusCode == 200) { 
          // Notice I use "res" not "response" for returning response 
          res.writeHead(200, {'Content-Type': "application/json"}); 
          res.end(body); 
         } 
         else { 
          res.writeHead(404, {'Content-Type': "application/json"}); 
          res.end(JSON.stringify({'Error': err})); 
         } 
        }); 
    } 
    else if (req.method == 'GET') { 
     proxy.web(req, res, { target: 'http://localhost/9000' }, function(err) { 
      console.log(err) 
     }); 
    } 

Porty 10500 i 9000 są arbitralne i moim kodem dynamicznie przydzielać je w oparciu o Usługi I gospodarza. To nie dotyczy PUT i może być mniej wydajne, ponieważ tworzę inną odpowiedź zamiast manipulować bieżącą.

Powiązane problemy