2013-04-04 19 views
6

Ecco la versione del modulo che sto usando:Come utilizzare node-http-proxy per il routing da HTTP a HTTPS?

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

un webservice chiama in mia macchina e il mio compito è quello di delega la richiesta a un URL diverso e host con un parametro di query aggiuntiva in base al contenuto del Il corpo di richiesta:

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

Ma io continuo a correre in errori come: An error has occurred: {"code":"ECONNRESET"}

Qualcuno ha un'idea su ciò che deve essere fissato qui?

risposta

6

Questo ha fatto il trucco per me:

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

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

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

Grazie Aaron, che è la prima risposta utile che ho ricevuto su questo argomento in un tempo lungo e uno che funziona davvero! – pulkitsinghal

2

Per inoltrare tutte le richieste che arrivano sulla porta 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) 

Esempio ispirato https://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/proxy-http-to-https.js.

+0

grazie, le cose sono cambiate molto dal 4 apr 1013 – pulkitsinghal

+1

Sì, è per questo che ho postato una risposta aggiornata. –

0

Dopo alcuni tentativi ed errori, questo ha funzionato per 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); 
Problemi correlati