如何使用node-http-proxy进行HTTP到HTTPS路由?

时间:2013-04-04 01:22:17

标签: node.js http-proxy node-http-proxy

这是我正在使用的模块版本:

$ npm list -g | grep proxy
├─┬ http-proxy@0.10.0

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

这样的错误

任何人都知道需要在这里修复什么?

3 个答案:

答案 0 :(得分:8)

这对我有用:

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

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

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

答案 1 :(得分:2)

将端口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)

https://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/proxy-http-to-https.js启发的示例。

答案 2 :(得分:0)

经过一些反复试验,这对我有用:

03-324-27216|14, , ,"1,=,14',0"",X,3.500""",14.000,,49, ,, ,3.500, ,MF,AU,210,AS,M,,0, ,05-01-15, ,,
03-362-50502|14,24543, ,"1,=,21',6"",X,3.625""",21.000,,77, ,, ,3.625, ,BF,GP,212, ,, ,, , ,