如何使用node-http-proxy进行HTTP到HTTPS路由? [英] How to use node-http-proxy for HTTP to HTTPS routing?

查看:139
本文介绍了如何使用node-http-proxy进行HTTP到HTTPS路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here's the module version that I'm using:

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

一个Web服务调用我的机器,我的任务是根据请求正文的内容,将请求代理到另一个url和主机,并带有一个附加的查询参数:

A webservice calls into my machine and my task is to proxy the request to a different url and host with an additional query parameter based on the contents of the request's body:

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

每个人都有什么需要解决的想法吗?

Anyone have an idea about what needs to be fixed here?

推荐答案

这对我有用:

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

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

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

这篇关于如何使用node-http-proxy进行HTTP到HTTPS路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆