How to use node-http-proxy for HTTP to HTTPS routing?

三世轮回 提交于 2019-11-30 14:31:14

This did the trick for me:

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

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

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

To forward all requests that come in on port 3000 to 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)

Example inspired by https://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/proxy-http-to-https.js.

After some trial and error, this worked for 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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!