2013-04-04 42 views
6

Dưới đây là phiên bản mô-đun mà tôi đang sử dụng:Cách sử dụng nút-http-proxy để định tuyến HTTP sang HTTPS?

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

Một webservice gọi vào máy của tôi và nhiệm vụ của tôi là để proxy yêu cầu đến một url khác nhau và máy chủ với một tham số truy vấn bổ sung dựa trên các nội dung của cơ thể theo yêu cầu của:

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

Nhưng tôi tiếp tục chạy vào các lỗi như: An error has occurred: {"code":"ECONNRESET"}

Bất cứ ai có một ý tưởng về những gì cần phải được cố định ở đây?

Trả lời

6

này đã làm các trick cho tôi:

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

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

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

Cảm ơn bạn Aaron, đó là câu trả lời hữu ích đầu tiên tôi đã nhận được về chủ đề này trong một thời gian dài và một trong đó thực sự hoạt động! – pulkitsinghal

2

Để chuyển tiếp tất cả các yêu cầu mà đi theo trên cổng 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) 

Ví dụ lấy cảm hứng từ https://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/proxy-http-to-https.js.

+0

cảm ơn, mọi thứ đã thay đổi rất nhiều kể từ apr 4 1013 – pulkitsinghal

+1

Vâng, đó là lý do tôi đăng câu trả lời cập nhật. –

0

Sau một số thử và sai, điều này đã làm việc cho tôi:

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); 
Các vấn đề liên quan