动态生成子域

时间:2014-06-17 06:59:12

标签: node.js angularjs express mean-stack

我正在开发一个MEAN应用程序,该应用程序为人们提供了自己独特的子域,作为其注册过程的一部分。我该怎么做?

我对我使用的云服务持开放态度,例如Digital Ocean给我留下了深刻的印象,但同样可能是AWS。只要它是可扩展的。

那么如何在Bob与我们签约时如何生成bobsmith.nicksamazingnewapp.com?并且让他能够马上使用它?

2 个答案:

答案 0 :(得分:2)

虚拟主机是可行的方法。看看我前几天遇到的这个新的librabry。您只是使用vhost中间件。 https://github.com/expressjs/vhost

以下是其网站的代码示例

// create main app
var app = connect()

// add vhost routing to main app for mail
app.use(vhost('mail.example.com', mailapp))

// route static assets for "assets-*" subdomain to get
// around max host connections limit on browsers
app.use(vhost('assets-*.example.com', staticapp))

fyi,如果你使用的是mean.io,那么你需要在config / express.js文件中添加你的middlware

答案 1 :(得分:1)

我认为最好的解决方案是为子域设置一个通配符,以便任何东西* .nicksamazingnewapp.com将被捕获,然后服务器端可以决定它将指向的位置

app.get('/', function(req, res) {

  var hostname = req.headers.host.split(":")[0];

  if(hostname == "sub1.domain.com")
    res.send("this is sub1 response!");
  else if(hostname == "sub2.domain.com")
    res.send("this is sub2 response!");

});

代码的积分转到@Jazor

您还可以尝试使用模块https://www.npmjs.org/package/subdomain