javascript - 使用 Express Backend 动态更改 React Js 应用程序的元标记
问题描述
我一直在研究动态更改我的 react Js 应用程序的元标记。我尝试了很多解决方案,例如 Helmet,但我没有成功。如果有人能给我一个关于这项任务的清晰算法,我会很高兴。我有两个后端,一个在 Express 中,另一个在 PHP 中。我专门为此任务创建了 Express 后端,但是当我将链接分享到 Facebook 等其他平台时,我还没有成功更改元标记。
我目前的实施。前端:我的 Index.html 里面 public 有
<title>$OG_TITLE</title>
<meta name="description" content="$OG_DESCRIPTION" />
<meta property="og:title" content="$OG_TITLE" />
<meta property="og:description" content="$OG_DESCRIPTION" />
<meta property="og:image" content="$OG_IMAGE" />
快速后端
const express = require('express');
const cors = require("cors");
const path = require('path');
const fs = require('fs');
const app = express();
app.use(cors());
const port = process.env.PORT || 2800;
app.use('/api/dynamicmeta', (req, response)=>{
const filePath = path.resolve(__dirname, './build', 'index.html')
fs.readFile(filePath, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
data = data.replace(/\$OG_TITLE/g, 'Contact Page');
data = data.replace(/\$OG_DESCRIPTION/g, "Contact page description");
result = data.replace(/\$OG_IMAGE/g, 'https://i.imgur.com/V7irMl8.png');
response.send(data);
});
});
app.use(express.static(path.resolve(__dirname, './build')));
app.get('*', function(request, response) {
const filePath = path.resolve(__dirname, './build', 'index.html');
response.sendFile(filePath);
});
app.listen(port, () => console.log(`listening on port ${port}!..`));
module.exports = {
app
}
在这一点上,我对如何继续感到困惑,因为我的构建标题没有改变。
解决方案
推荐阅读
- reactjs - reactjs - 类父组件调用函数子组件方法
- sql - 如何从 MariaDB 中检索任何日期格式为 YYYY-MM-DD 的数据?
- asp.net - 排序后的隐藏字段
- html - 四个大小相同的图像的网格 - 无法让它们顶部对齐
- python - R Studio Python 集成错误 - OSError: [WinError 6] 句柄无效 & TypeError: 'module' 类型的对象没有 len()
- vbscript - 将在所有子文件夹上执行的 VBS 脚本
- java - 如何在android中创建两级列表下拉组件
- docker - 使用映射卷到 kong 容器的持久数据
- java - 第一个没有渲染的 Java 游戏
- python - 使用 python 或 api 远程访问您的 android 设备