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

Here is the version of the module I'm using:

$ npm list -g | grep proxy β”œβ”€β”¬ http-proxy@0.10.0 

A web service is called on my computer, and my task is to proxy the request to another URL and host with an additional request parameter based on the contents of the request 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 constantly encounter errors: An error has occurred: {"code":"ECONNRESET"}

Does anyone have an idea on what needs to be fixed here?

+6
source share
3 answers

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); 
+6
source

To redirect all requests entering 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 .

+2
source

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); 
0
source

All Articles