多个域,单节点(快速)应用程序 [英] Multiple domains, single node (express) app

查看:110
本文介绍了多个域,单节点(快速)应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建简单的节点应用程序,用户可以在其中创建一个配置文件。默认情况下,他的个人资料的网址应该是 - user1.myappname.com,但是当用户填写自定义域输入(并将此域指向我的应用程序IP地址)时,他应该可以使用这样的自定义域,如: p>

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



这里有人有使用express.js实现的经验吗?我的意思不仅是自定义域,还包括子域。



谢谢
-M

解决方案

路径是一样的,无论域名如何,这很简单。从发送到您的Express路由方法的请求中获取主机名,然后执行所需的任何查找。节点不在乎域是什么,只要您的域具有子域的CNAME,并且自定义域指向与myappname.com相同的IP地址,节点将以相同的方式响应所有请求。



例如,在/ someaction路线中:

  get('/ someaction',function(req,res){
hostName = req.header('host');
//从基于hostName的数据库查询信息,然后输出....
});


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

解决方案

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 ....
});

这篇关于多个域,单节点(快速)应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆