Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use node-http-proxy for HTTP to HTTPS routing?

Here's the module version that I'm using:

$ npm list -g | grep proxy
├─┬ [email protected]

A webservice calls into my machine and my task is to proxy the request to a different url and host with an additional query parameter based on the contents of the request's body:

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

httpProxy.createServer(function (req, res, proxy) {
  // my custom logic
  var fullBody = '';
  req.on('data', function(chunk) {
      // append the current chunk of data to the fullBody variable
      fullBody += chunk.toString();
  });
  req.on('end', function() {
      var jsonBody = form2json.decode(fullBody);
      var payload = JSON.parse(jsonBody.payload);
      req.url = '/my_couch_db/_design/ddoc_name/_update/my_handler?id="' + payload.id + '"';

      // standard proxy stuff
      proxy.proxyRequest(req, res, {
        changeOrigin: true,
        host: 'my.cloudant.com',
        port: 443,
        https: true
      });
  });
}).listen(8080);

But I keep running into errors like: An error has occurred: {"code":"ECONNRESET"}

Anyone have an idea about what needs to be fixed here?

like image 779
pulkitsinghal Avatar asked Apr 04 '13 01:04

pulkitsinghal


People also ask

What is NPM HTTP proxy?

node-http-proxy. node-http-proxy is an HTTP programmable proxying library that supports websockets. It is suitable for implementing components such as reverse proxies and load balancers.

What is HTTP createServer in node JS?

The http. createServer() method turns your computer into an HTTP server. The http. createServer() method creates an HTTP Server object. The HTTP Server object can listen to ports on your computer and execute a function, a requestListener, each time a request is made.

What is a HTTP proxy?

The HTTP-proxy is a high-performance content filter. It examines Web traffic to identify suspicious content that can be a virus or other type of intrusion. It can also protect your HTTP server from attacks. WatchGuard recommends you use HTTP Proxy policies for any HTTP traffic between your network and external hosts.


3 Answers

To forward all requests that come in on port 3000 to https://google.com:

const https = require('https')
const httpProxy = require('http-proxy')

httpProxy.createProxyServer({
  target: 'https://google.com',
  agent: https.globalAgent,
  headers: {
    host: 'google.com'
  }
}).listen(3000)

Example inspired by https://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/proxy-http-to-https.js.

like image 98
Ryan Walls Avatar answered Oct 06 '22 09:10

Ryan Walls


This did the trick for me:

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

var options = {
  changeOrigin: true,
  target: {
      https: true
  }
}

httpProxy.createServer(443, 'www.google.com', options).listen(8001);
like image 28
Aaron Shafovaloff Avatar answered Oct 06 '22 09:10

Aaron Shafovaloff


After some trial and error, this worked for me:

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

var KEY  = 'newfile.key.pem';
var CERT = 'newfile.crt.pem';

httpProxy.createServer({
  changeOrigin: true,
  target: 'https://example.com',
  agent: new https.Agent({
    port: 443,
    key: fs.readFileSync(KEY),
    cert: fs.readFileSync(CERT)
  })
}).listen(8080);
like image 41
raine Avatar answered Oct 06 '22 09:10

raine