NodeJS - HTTPS / HTTP代理服务器设置

时间:2014-01-17 23:19:19

标签: node.js http https proxy

我目前正在尝试使用NodeJS设置HTTP / HTTPS代理服务器。使用此gist的示例,这就是我所拥有的。

var fs = require('fs'),
  http = require('http'),
  https = require('https'),
  httpProxy = require('http-proxy');

var isHttps = true; // do you want a https proxy?

var options = {
  https: {
    key: fs.readFileSync('/home/ubuntu/key.key'),
    cert: fs.readFileSync('/home/ubuntu/crt.crt')
  }
};

// this is the target server
var proxy = new httpProxy.HttpProxy({
  target: {
    host: '127.0.0.1',
    port: 11612
  }
});

if (isHttps)
  https.createServer(options.https, function(req, res) {
    console.log('Proxying https request at %s', new Date());
    proxy.proxyRequest(req, res);
  }).listen(443, function(err) {
    if (err)
      console.log('Error serving https proxy request: %s', req);

    console.log('Created https proxy. Forwarding requests from %s to %s:%s', '443', proxy.target.host, proxy.target.port);
  });
else
  http.createServer(options.https, function(req, res) {
    console.log('Proxying http request at %s', new Date());
    console.log(req);
    proxy.proxyRequest(req, res);
  }).listen(80, function(err) {
    if (err)
      console.log('Error serving http proxy request: %s', req);

    console.log('Created http proxy. Forwarding requests from %s to %s:%s', '80', proxy.target.host, proxy.target.port);
  });

问题是,当我在我的Ubuntu服务器上运行时,这是我得到的错误。有点失落。

/home/ubuntu/prox.js:16
var proxy = new httpProxy.HttpProxy({
            ^
TypeError: undefined is not a function
    at Object.<anonymous> (/home/ubuntu/prox.js:16:13)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3
17 Jan 23:18:34 - [nodemon] app crashed - waiting for file changes before starting...

1 个答案:

答案 0 :(得分:2)

您是否尝试过以下操作,可能有所帮助,这是来自他们的git中心页面。

var proxy = httpProxy.createProxyServer(options);