0

I used Node-http-proxy to proxy my request from port sample.com:80 to port sample.com:8080 with sample below:

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

httpProxy.createServer(8080, 'localhost').listen(80);

http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9000);

When I do: curl with POST to sample.com:80, I get 403 Forbidden response. But when I do curl with GET, 301 permanent redirect. My question is:

Is it possible to make POST and GET behaves the same, which always return 200 status code instead of 403 or 301?

Thanks

4

1 回答 1

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

fmt.sep();
console.log();
fmt.title("\033[1;36mPROXY SERVER STARTED\033[0m");
console.log();
fmt.field("\033[1;34mPROXY PORT\033[0m", 80);
fmt.field("\033[1;34mTARGET PORT\033[0m", 8080);
fmt.field("\033[1;34mTARGET ADDRESS\033[0m", '127.0.0.1');

var server = httpProxy.createServer(function(req, res, proxy)
{
    proxy.proxyRequest(req, res, { host: '127.0.0.1', port: 8080, buffer: httpProxy.buffer(req) });

    var reqBody = '';

    req.on('data', function(chunk)
    {
        reqBody += chunk;
    });

    req.on('end', function()
    {
        console.log();
        fmt.sep();
        console.log();
        fmt.title("\033[1;36mREQUEST FROM SHARED CLOUD\033[0m");
        console.log();
        fmt.dump(req.connection.remoteAddress, "\033[1;35mADDRESS\033[0m");
        console.log();
        fmt.msg("\033[1;35mHEADERS\033[0m :");
        fmt.dump(req.headers);
        console.log();
        fmt.msg("\033[1;35mBODY\033[0m :");
        fmt.dump(reqBody);
        console.log();
    });
});

server.proxy.on('proxyError', function(err, req, res)
{
    fmt.title("\033[1;36mPROXY STATUS\033[0m");
    console.log();
    fmt.dump('failed to proxy on port ' + 8080, "\033[1;31mERROR\033[0m");
    res.end();
});

server.proxy.on('end', function()
{
    fmt.title("\033[1;36mPROXY STATUS\033[0m");
    console.log();
    fmt.dump('request was proxied successfully', "\033[1;33mSTATUS\033[0m");
});

server.listen(80);
于 2013-04-25T18:06:45.140 回答