如何使用node-http-proxy进行HTTP到HTTPS的路由?

以下是我正在使用的模块版本:

$ npm list -g | grep proxy ├─┬ http-proxy@0.10.0 

一个web服务调用我的机器,我的任务是代理请求到一个不同的url和主机与一个额外的查询参数基于请求的内容:

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

但是我仍然An error has occurred: {"code":"ECONNRESET"}错误: An error has occurred: {"code":"ECONNRESET"}

任何人都有一个想法,需要在这里修复?

       

网上收集的解决方案 "如何使用node-http-proxy进行HTTP到HTTPS的路由?"

这为我做了诡计:

 var httpProxy = require('http-proxy'); var options = { changeOrigin: true, target: { https: true } } httpProxy.createServer(443, 'www.google.com', options).listen(8001); 

要将端口3000上的所有请求转发到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) 

例子受到https://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/proxy-http-to-https.js的启发。

经过一些试验和错误,这对我工作:

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