2013-04-04 66 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); 
相關問題