2013-02-27 36 views
6

Sto provando a utilizzare il nodo-http-proxy come proxy inverso, ma non riesco a far funzionare le richieste POST e PUT. Il file server1.js è il proxy inverso (almeno per le richieste con l'url "/ forward-this") e server2.js è il server che riceve le richieste proxy. Per favore, spiegami cosa sto facendo in modo errato.Come invertire le richieste POST e PUT del client proxy utilizzando il nodo-http-proxy

Ecco il codice per 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(); 
    } 
} 

Ed ecco il codice per 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(); 
} 

risposta

4

http-proxy dipende dalle data e end eventi per richieste/PUT POST . La latenza tra il momento in cui il server1 riceve la richiesta e quando è proxy indica che l'http-proxy manca completamente quegli eventi. Hai due opzioni qui per farlo funzionare correttamente - puoi buffer the request oppure puoi usare un routing proxy invece. Il proxy di routing sembra il più appropriato in questo caso, dal momento che devi solo eseguire il proxy di un sottoinsieme di richieste. Ecco le server1.js rivisti:

// 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

Che ha funzionato perfettamente. Grazie! –

+0

Sebbene sia una vecchia risposta, per farlo funzionare dovresti sostituire 'var proxy = new httpProxy.RoutingProxy();' con 'var proxy = httpProxy.createProxyServer ({});' – Saber

1

Ecco la mia soluzione per il proxy delle richieste POST. Questa non è la soluzione ideale, ma funziona ed è facile da capire.

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) 
     }); 
    } 

Le porte 10500 e 9000 sono arbitrari e nel mio codice ho dinamicamente assegnare agli interessati in base ai servizi che host. Questo non ha a che fare con PUT e potrebbe essere meno efficiente perché sto creando un'altra risposta invece di manipolare quella corrente.

Problemi correlati