node.js - 我们如何使用 vhost 和 nodejs 添加子域 dynamicllay
问题描述
我想根据用户用户名创建一个子域,因为每个用户的用户名都是唯一的
我手动创建子域,但我不知道如何通过用户名创建它
const subdomain = express();
const domain = "localhost";
app.use(vhost(`username.${domain}`, subdomain)); //here username is manually i entered
我们如何通过使用 API 从 DB 获取用户名并创建子域来做到这一点
解决方案
您可以使用node-awesome-router 这是示例 ho 来配置带有子域的路由,而无需任何 oder(主机、nginx 等)设置
const router = NodeAwesomeRouter({
app,
routes: [{
key: '/path',
routes: {'/r1': ()=>{}, '/r2': () => {}, ... },
subdomain: 'users' // user.domain.com
}, {
key: '/path2',
routes: {'/r1': ()=>{}, '/r2': () => {}, ... },
subdomain: 'dev' // dev.domain.com
}],
})
推荐阅读
- django - Django inline_formset - 确保至少选择一项
- javascript - forEach 中的 Http 请求 - NodeJS
- vb.net - 使用 Linq 对数据表进行分组会产生错误:“方法定义
在这种情况下无法访问” - mysql - 远程服务器中的Mysql错误。但在我的本地机器上工作正常
- python - 从 VBA 调用 Python (Winwrap)
- html - 如何使双击而不是单击以角度打开文件
- c# - Linq 获取属性与 int 数组中的任何值匹配的项目
- python - 从 3d numpy 数组中删除元素
- android - 强制使用 Android 原生键盘而不是自定义键盘
- regex - 从电子邮件地址中提取名字和姓氏