reactjs - 将 react 应用程序中的 html 代码插入到 express 中
问题描述
我有一个包含以下域(通配符子域)的快速应用程序
www.example.com
subdomain1.example.com
subdomain2.example.com
我在cdn.example.com也有一个反应应用程序
我需要将该反应应用程序加载到 subdomain2.example.com
基本上我在快速应用程序中有一些逻辑检查子域是否等于 subdomain1 然后它应该加载反应应用程序,否则只是在页面中打印一些东西。
我正在考虑做这样的事情来从 cdn 中读取 index.html 并将其返回到 express 应用程序中
router.get('/', (req, res) => {
if (req.subdomains[0] === 'subdomain1') {
request('http://cdn.example.com/index.html', function (error, response, body) {
return res.status(200).send(body)
});
} else {
return res.status(200).send('hello world')
}
});
它有效,但我认为这不是正确的方法。我只找到了从 react 应用程序中直接指向 index.html 的域示例。
我最大的问题是 React 应用程序在每次构建时都会更改哈希值。这就是为什么我只是抓取整个 html React index.html 文件。
解决方案
您可以重定向:
router.get('/', (req, res) => {
if (req.subdomains[0] === 'subdomain1') {
res.redirect('http://cdn.example.com/index.html')
} else {
return res.status(200).send('hello world')
}
});
推荐阅读
- python - 将 pandas 管道中的一系列分隔(用等号)分成多列
- karate - 为每个测试运行者在单独的 karate_report 文件夹中生成报告
- swift - 我如何快速实时获得 Process() 函数的输出?
- heroku-postgres - Heroku Postgres 附件
- reactjs - 如何在 MobX 商店更新上让 Snowpack 更新 HMR
- maven - 恢复从 maven clean 删除的目标文件夹文件
- c# - 如何在C#中修改Kafka保留主题消息的时间?
- regex - 如何提取真实姓名,名字,姓氏,并从具有首字母缩写词,贵族头衔,学术头衔/学位的名称中创建昵称
- java - Android 中哪种 RSA 和 AES 转换效果最好
- angular - Sonarqube 没有覆盖导入模块