2014-10-16 74 views
1

我有快速運行的節點應用程序。我想設置兩種不同的帳戶類型(一個用於買方,一個用於賣方),並讓他們可以通過不同的子域(例如buyers.example.com和sellers.example.com)訪問。我不知道如何在本地設置。我知道我可以編輯我的機器上的主機文件,以便*.localhost解析爲127.0.0.1,但這並不能解決我的問題。在這種情況下,buyers.localhost/loginsellers.localhost/login將路由到相同的地方。這是一個常見問題,如果是這樣,處理這個問題的標準方法是什麼?我有什麼選擇來分開處理兩種賬戶類型的邏輯?在快速應用程序上本地使用子域

+0

在['expressjs/vhost'(HTTPS看看.com/expressjs/vhost)([formerly](https://github.com/senchalabs/connect#middleware)'express.vhost()'和'connect.vhost()')。還相關:「[單節點.js http服務器接受多個主機名上的連接](http://stackoverflow.com/q/5262127),」「[我如何在同一IP /服務器上託管多個Node.js站點不同的域?](http://stackoverflow.com/q/19254583),「」[如何安裝express.js子應用程序?](http://stackoverflow.com/q/8514106)「和」[ nodejs中的子域](http://stackoverflow.com/q/13208145)「 – 2014-10-16 16:40:35

回答

2

首先補充一點:

app.get('*', function(req, res, next){ 
    if (req.headers.host == 'buyers.localhost:5000') { //Port is important if the url has it 
     req.url = '/buyers' + req.url; 
    } 
    else if(req.headers.host == 'sellers.localhost:5000') { 
     req.url = '/sellers' + req.url; 
    } 
    next(); 
}); 

然後:

app.get('/login', function(){ 
    //Default case, no subdomain 
}) 

app.get('/buyers/login', function(){ 
    //Buyers subdomain 
}) 

app.get('/sellers/login', function(){ 
    //Sellers subdomain 
}) 

這在這裏學到:// github上:https://groups.google.com/forum/#!topic/express-js/sEu66n8Oju0

相關問題