我需要使用快递服务器以动态方式将og:title的内容更改为index.blade.php。如您所知,更改标签内容的唯一方法是使用服务器端渲染,因为Facebook如果由React头盔组件添加了og标签,则它们无法读取。所以,这就是我所做的:
在index.balde.php中,我添加了以下几行:
<meta property="og:title" content="$OG_TITLE">
<meta property="og:description" content="$OG_DESCRIPTION">
然后,我创建了server.js文件,以便通过服务器动态更改标签的内容:
var express = require('express');
var app = express();
const path = require('path');
const fs = require('fs')
app.get('/profile', function(request, response) {
console.log("Entered the logic");
const filePath = path.resolve(__dirname, '../views', 'index.blade.php');
// read in the index.html file
fs.readFile(filePath, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
let result = null;
// replace the special strings with server generated strings
data = data.replace(/\$OG_TITLE/g, 'PROFILE TITLE');
result = data.replace(/\$OG_DESCRIPTION/g, "PROFILE DESCRIPTIONS");
//result = data.replace(/\$OG_IMAGE/g, 'https://i.imgur.com/V7irMl8.png');
response.send(result);
});
});
app.listen(5000, function(){
console.log('listening on *:' + 5000);
});
结果,当我打开www.testurl.com:5000/profile时,标记的内容已成功更改,但是我的问题是如何在不添加端口号的情况下更改标记的内容url,因为请求的链接应该没有端口号。用户不需要在URL中添加端口号。 无论如何,react / express中是否有任何内容可以让服务器知道,当用户调用www.testurl.com/profile时,应该调用服务器上端口500上的所有事件侦听器(以其他方式访问没有端口号app.get( '/ profile',...)应该被调用)?还是有一种方法可以将带有端口号的url调用到基本组件中,以使每次用户打开应用程序内的任何页面时都会发生此事件?
我真的很需要帮助,因为我花了很多时间来寻找解决方案。谢谢。