Multiple domains, single node (express) app

*爱你&永不变心* 提交于 2019-12-03 21:09:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!