2016-12-14 15 views
1

我想讓多個nodejs應用程序都監聽代理本地計算機上某個URL的不同端口。Nodejs - 將多個應用程序代理到本地的單個url

一個例子是

本地主機:3000 - > mysite.dev 本地主機:3030 - > mysite.dev/api

它將使開發本地符合我的生產設置和我的無國籍幫助極大認證setep。我認爲hotel將成爲我想要的解決方案,但它並不完全符合我的要求。

如果可能,我想避免在本地使用nginx。

回答

1

你可以嘗試http-proxy模塊,您可以指定代理作爲

var http = require('http'), 
    httpProxy = require('http-proxy'); 
// 
// Create your proxy server and set the target in the options. 
// 
httpProxy.createProxyServer({target:'http://localhost:9000'}).listen(8000); // See (†) 

// 
// Create your target server 
// 
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); 

他們可以在這裏https://github.com/nodejitsu/node-http-proxy

+0

這是偉大的好的文檔,謝謝。這正是我正在尋找的。 – imns

+0

你能舉出一個例子來回答這個問題嗎,這個問題包括由URI分隔的80端口上的2個獨立代理? – Sawtaytoes

相關問題