【问题标题】:Router in Node http proxyNode http代理中的路由器
【发布时间】:2016-05-06 12:53:19
【问题描述】:

谁能解释一下选项中的路由器在这段代码中的作用。我从博客中得到了这段代码。我正在尝试实现节点 http-proxy。

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

//
//Leave out the hostnameOnly field this time, or set it to false...
//
var options = {  
  router: {
    'domainone.com/appone': '127.0.0.1:9000',
    'domainone.com/apptwo': '127.0.0.1:9001',
    'domaintwo.net/differentapp': '127.0.0.1:9002'
  }
}


//
//...and then pass in your options like last time.
//
var proxyServer = httpProxy.createServer(options).listen(80);

//
// ...and a simple http server to show us our request back.
//
http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9000);

【问题讨论】:

  • 代理表已被弃用,取而代之的是add-ons

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


【解决方案1】:

代理库将接收所有传入请求并尝试将其与您的路由器表中的规则匹配。假设它找到匹配项,它将将该请求转发到与您提供的 DNS 名称关联的 IP 地址。

例如,去往 domainone.com/appone 的请求将被转发到 127.0.0.1:9000

我在这里看到的一个问题是您正在侦听端口 9000,并且您的第一条规则重新路由到 127.0.0.1:9000

【讨论】:

  • 但是当我输入 domainone.com/appone.它没有显示 localhost:9000 的页面。
  • 在浏览器中输入该内容不会自动将您重定向到 localhost:9000。您需要编辑本地主机文件以将 domainone.com 发送到您的代理应用程序正在运行的任何 IP 地址
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
相关资源
最近更新 更多