Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple domains, single node (express) app

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

like image 762
Martin Šnajdr Avatar asked Feb 03 '12 08:02

Martin Šnajdr


People also ask

Can you link multiple domains to one?

With most registrars, it's easy to forward multiple domains to your website so you can simply create one site and then redirect visitors who type one of your other domain names to that one website.

Why should you separate express APP and server in Nodejs?

Faster testing execution. Getting wider coverage metrics of the code. Allows deploying the same API under flexible and different network conditions. Better separation of concerns and cleaner code.

Is Express better than node?

js: Express is a small framework that sits on top of Node. js's web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application's functionality with middle ware and routing.


1 Answers

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 ....
});
like image 111
Billy Cravens Avatar answered Sep 22 '22 18:09

Billy Cravens