Multiple domains, single node application (express)

I am trying to create a simple node application where a user can create a profile. By default, the URL of his profile should look like user1.myappname.com, but when the user fills in the user input of the domain (and points this domain to the IP address of my application), he should be able to use this custom domain, for example:

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

Does anyone have any experience implementing this with express.js? I mean not only user domains, but also subdomains.

Thanks. -M

+4
source share
1 answer

Since your paths are the same no matter which domain, it is simple. Take the hostname from the query passed to the express route methods, and then do any search you need. Node does not care what a domain is, and as long as your domain has a CNAME for your subdomains, and user domains point 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 .... }); 
+6
source

Source: https://habr.com/ru/post/1394558/


All Articles