2013-04-04 29 views
6

下面是我使用的模块版本:如何使用node-http-proxy进行HTTP到HTTPS路由?

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

一个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"}

任何人有需要被固定在这里有什么想法?

回答

6

这并获得成功对我来说:

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

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

httpProxy.createServer(443, 'www.google.com', options).listen(8001); 
+1

谢谢阿龙,这是我在很长一段时间和一个实际工作收到关于这一主题的第一个有用的答案! – pulkitsinghal 2013-09-09 20:31:24

2

要转进来端口3000https://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启发。

+0

谢谢,自4月4日以来事情发生了很大变化1013 – pulkitsinghal 2014-05-22 16:44:28

+1

是的,这就是为什么我发布了更新的答案。 – 2014-05-23 00:36:42

0

一些试验和错误之后,这个工作对我来说:

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); 
相关问题