8

node-http-proxy をリバース プロキシとして使用しようとしていますが、POST および PUT リクエストが機能しないようです。ファイル server1.js はリバース プロキシ (少なくとも "/forward-this" という URL を持つリクエストの場合) であり、server2.js はプロキシされたリクエストを受信するサーバーです。私が間違っていることを説明してください。

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

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

3 に答える 3

5

http-proxy は、POST / PUT リクエストのdataおよびイベントに依存します。endserver1 がリクエストを受信して​​からプロキシされるまでの待ち時間は、http-proxy がこれらのイベントを完全に見逃していることを意味します。これを正しく機能させるには、2 つのオプションがあります。リクエストをバッファリングするか、代わりにルーティング プロキシを使用することができます。リクエストのサブセットのみをプロキシする必要があるため、ここではルーティング プロキシが最も適切と思われます。改訂された 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();
}
于 2013-02-28T07:41:09.327 に答える