2016-05-06 95 views
0

任何人都可以請解釋什麼路由器的選項在這段代碼中做的。我從博客中獲得了這些代碼。我試圖實現節點http代理。路由器在節點http代理

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

+0

代理表已被棄用,以支持[add-on](https://github.com/nodejitsu/node-http-proxy#proxytable-api)。 – robertklep

回答

0

代理庫將採取所有傳入的請求,並嘗試在你的路由器表的規則相匹配。假設它找到匹配項,它會將該請求轉發到與您提供的DNS名稱相關聯的IP地址。

例如,請求將domainone.com/appone將被轉發到127.0.0.1:9000

的一個問題,我在這裏看到的是,你是偵聽端口9000,和您的第一個規則重新路由到127.0.0.1:9000

+0

但是,當我輸入domainone.com/appone。它沒有顯示localhost:9000的頁面。 – bharadwaj

+0

在瀏覽器中輸入該內容不會自動將您重定向到localhost:9000。您需要編輯本地主機文件以將domainone.com發送到您的代理應用程序正在運行的任何IP地址 – tier1