问题
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
回答1:
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 ....
});
来源:https://stackoverflow.com/questions/9125577/multiple-domains-single-node-express-app