簡體   English   中英

NodeJS - HTTPS / HTTP代理服務器設置

[英]NodeJS - HTTPS/HTTP Proxy Server Setup

我目前正在嘗試使用NodeJS設置HTTP / HTTPS代理服務器。 使用這個要點的例子,這就是我所擁有的。

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

您是否嘗試過以下操作,可能有所幫助,這是來自他們的git hub頁面。

var proxy = httpProxy.createProxyServer(options);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM