首页 > 解决方案 > 将 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 文件。

标签: reactjsexpress

解决方案


您可以重定向:

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

推荐阅读