5

I'm trying to create simple node app, where the user can create a profile. Defaultly the url to his profile should be like - user1.myappname.com, but when the user fills a custom domain input (and points this domain to my app IP address), he should be able to use this custom domain like:

usercustomdomain.com => user1.myappname.com usercustomdomain.com/someaction => user1.myappname.com/someaction

Does anybody here have an experience with implementing this with express.js? I mean not only custom domains but also subdomains.

Thank you -M

Martin Šnajdr
  • 182
  • 4
  • 14

1 Answers1

7

Since your paths are the same no matter what the domain, this is simple. Grab the host name from the request passed into your Express route methods, and then do whatever lookup you need. Node doesn't care what the domain is, and as long as your domain has CNAMEs for your subdomains, and custom domains are pointed to the same IP address as myappname.com, node will respond to all requests in the same way.

For example, in your /someaction route:

app.get('/someaction', function(req,res) {
    hostName = req.header('host');
    // lookup info from database based on hostName, then output it ....
});
Billy Cravens
  • 1,643
  • 10
  • 15
  • 1
    I'd like to note, that cookies will not go crossdomain (let subdomains access cookies from main domain), if you don't set the path to let that happen. This can be done easily, but you must be able to trust the subdomains, because they could read/steal cookie/session information and hijack it. – japrescott Feb 03 '12 at 12:54
  • @japrescott What if the administration for every user will be located on www.myappname.com/admin? Maybe it solves the problem with cookies, because they would not be in conflict with subdomains or custom domains... – Martin Šnajdr Feb 03 '12 at 13:44
  • @MartinSnajdr, thats not a problem per se. But if you have an other website at someother.myappname.com and don't want that the user would need to login again, you'll need to trust that "someother" subdomain – japrescott Feb 04 '12 at 09:40