简体   繁体   中英

NodeJS - HTTPS/HTTP Proxy Server Setup

I'm currently trying to setup an HTTP/HTTPS proxy server using NodeJS. Using the example of this gist , this is what I have.

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

Issue is, when I run it on my Ubuntu server, this is the error I'm getting. Kinda lost.

/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...

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

var proxy = httpProxy.createProxyServer(options);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM