1

I am working on a MEAN application which provides people with their own unique sub-domain as part of their sign-up process. How to I do this?

I am open minded regarding what cloud services I use, I am impressed by Digital Ocean for example but it could equally be AWS. So long as it is scalable.

So how can you generate bobsmith.nicksamazingnewapp.com for example when Bob signs up with us? and for him to be able to use it right-away?

Christian P
  • 12,032
  • 6
  • 60
  • 71
Nick Lewis
  • 772
  • 1
  • 10
  • 20
  • You could run Nginx or Apache as a proxy, then rewrite the request to go from `sub.domain.com` to `domain.com/sub`. – Ben Fortune Jun 17 '14 at 10:24

2 Answers2

2

Virtual hosts are the way to go. Have a look at this new librabry for express I came across the other day. You are simply using the vhost middleware. https://github.com/expressjs/vhost

Here is a code sample from their site

// 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, if you are using mean.io then you need to add your middlware in the config/express.js file

Yonatan Ellman
  • 286
  • 1
  • 4
1

I think the best solution would be to have a wildcard for subdomains so anything*.nicksamazingnewapp.com would be traped and then a server side you can decide to where it will point

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!");

});

credits for code goes to @Jazor

you can also try a module https://www.npmjs.org/package/subdomain

maurycy
  • 8,455
  • 1
  • 27
  • 44