2013-02-27 70 views
6

Tôi đang cố gắng sử dụng nút-http-proxy làm proxy ngược, nhưng dường như tôi không nhận được yêu cầu POST và PUT để hoạt động. Tệp server1.js là proxy ngược (ít nhất là đối với các yêu cầu có url "/ forward-this") và server2.js là máy chủ nhận các yêu cầu proxy. Hãy giải thích những gì tôi đang làm không chính xác.Cách đảo ngược yêu cầu POST & PUT của máy khách proxy bằng cách sử dụng nút-http-proxy

Dưới đây là các mã cho 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(); 
    } 
} 

Và đây là mã cho 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(); 
} 

Trả lời

4

http-proxy phụ thuộc vào dataend sự kiện cho các yêu cầu/PUT POST . Độ trễ giữa thời gian máy chủ1 nhận được yêu cầu và khi nó được ủy quyền có nghĩa là proxy http bỏ qua hoàn toàn các sự kiện đó. Bạn có hai tùy chọn ở đây để làm việc này hoạt động chính xác - bạn có thể buffer the request hoặc thay vào đó bạn có thể sử dụng routing proxy. Proxy định tuyến có vẻ thích hợp nhất ở đây vì bạn chỉ cần ủy quyền một tập hợp con các yêu cầu. Đây là server1.js sửa đổi:

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

Điều đó hoạt động hoàn hảo. Cảm ơn! –

+0

Mặc dù, nó là một câu trả lời cũ, để làm cho nó hoạt động, bạn nên thay thế 'var proxy = new httpProxy.RoutingProxy(); ' với' var proxy = httpProxy.createProxyServer ({}); ' – Saber

1

Đây là giải pháp của tôi để yêu cầu POST proxy. Đây không phải là giải pháp lý tưởng nhất, nhưng nó hoạt động và dễ hiểu.

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

Các cổng 105009000 là tùy tiện và trong mã của tôi, tôi tự động gán cho họ dựa trên các dịch vụ tôi chủ. Điều này không đối phó với PUT và nó có thể kém hiệu quả hơn vì tôi đang tạo một phản hồi khác thay vì thao tác với câu lệnh hiện tại.

Các vấn đề liên quan