【问题标题】:ExpressJS: SubdomainExpressJS:子域
【发布时间】:2023-03-09 03:25:02
【问题描述】:

在我的应用程序中,我正在创建多个用户,例如 user1,user2...,并希望为每个用户分配子域,例如 user1.xyz.com,user2.xyz.com ... 以下是应用程序的要求:

1) 在用户注册应用时动态创建这些子域

2) GET user1.xyz.com 应该调用类似/user1/home 的路由

我见过一个 npm 模块 https://www.npmjs.com/package/express-subdomain,但看起来它只适用于静态子域。

【问题讨论】:

    标签: node.js express routes subdomain


    【解决方案1】:

    找到满足要求的包:

    https://github.com/edwardhotchkiss/subdomain.

    app.use(subdomain({ base : 'localhost', removeWWW : true }));
    
    app.get('/subdomain/:name', Controller});    //GETs user1.localhost and user1 can be used as parameter using req.params.name
    

    例如。

    app.get('/subdomain/:name',function(req,res){
      var name = req.params.name;
      res.send(name);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2019-06-01
      • 2015-02-26
      相关资源
      最近更新 更多