【问题标题】:Using subdomains locally on an express app在快速应用程序上本地使用子域
【发布时间】:2014-12-12 02:37:48
【问题描述】:

我有 node 应用程序在 express 上运行。我想设置两种不同的帐户类型(一种用于买家,一种用于卖家)并让它们通过不同的子域(即买家.example.com 和卖家.example.com)访问。我不确定如何在本地进行设置。我知道我可以在我的机器上编辑主机文件,让*.localhost 解析为127.0.0.1,但这并不能解决我的问题。在这种情况下,buyers.localhost/login 和 sellers.localhost/login 将路由到同一个地方。这是一个常见问题吗?如果是这样,处理这个问题的标准方法是什么?我有哪些选项可以将处理两种帐户类型的逻辑分开?

【问题讨论】:

标签: node.js express subdomain


【解决方案1】:

首先添加这个:

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

在这里学习:https://groups.google.com/forum/#!topic/express-js/sEu66n8Oju0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-01
    • 2011-01-11
    • 2015-02-24
    • 2015-04-17
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    相关资源
    最近更新 更多