0

My code:

var path = require('path');
var util = require('util');
var http = require('http');
var fs = require('fs');
var httpProxy = require('http-proxy');


httpProxy.createProxyServer({
    target: 'http://localhost:9000',
    agent: https.globalAgent,
    headers: {
        host: 'localhost'
    }
})
.listen(8000);

util.puts('proxy server listening on port 8000');

http.createServer(function targetServer(req, res) {
    res.writeHead(302, { 'Location': '/' }); //Here is the 302
    util.puts('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));

    res.end();

})
.listen(9000);

util.puts('target server listening on port 9000');

when I make the request from my browser i get the following error:

Error: socket hang up

I have no idea what this error means. Can anyone help me figure this out?

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
dopatraman
  • 13,416
  • 29
  • 90
  • 154

1 Answers1

0

I did get it to work.
I had to do 2 things:

  1. Include follow-redirects package (npm install follow-redirects) And change my http initialization: To:
    var http = require('follow-redirects').http; from: var http = require('http');
  2. include the {changeOrigin: true} property in the CreateProxyServer: var proxy = httpProxy.createProxyServer({changeOrigin: true, toProxy : true, });