Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Multiple domains, single node (express) app

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 -, 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: => =>

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

Thank you -M

Problem courtesy of: Martin Snajdr


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, 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 ....
Solution courtesy of: Billy Cravens


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Multiple domains, single node (express) app


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription