Thursday, 11 May 2017

Node.js http-proxy with HTTPS

I have an application that creates a proxy server and returns the url requests when a page is accessed, but it works only for http web pages and when I'm trying to access a https address then I get Secure Connection Failed in the browser.

How can I change the following code to make it work for both http and https?

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

var proxy = httpProxy.createServer({
  target:'http://localhost:9000' 
});

proxy.listen(8000);

var http = require('http');
//
// Create your target server
//

http.createServer(function (req, res) {
  var options = {
    target: 'http://'+req.headers.host
  };
  console.log(req.url)
  console.log(req.headers.host)
  req.host = req.headers.host;
  proxy.web(req, res, options, function(err){
    console.log('err', err)
  });
}).listen(9000);

proxy.on('proxyReq', function (proxyReq, req, res) {
  console.log('request url', JSON.stringify(req.url, true, 2));
});



via Valip

No comments:

Post a Comment