我正在制作一个博客,并希望有一种方法,以推荐其他文章的基础上,人们目前正在看的。我不知道这通常是如何做到的,所以这是我的尝试。
首先,我有每个文章路由的JSON文件(articles.json),以及关于该文章的关键字。
[{
"linkToArticle": "/article1",
"keywords": ["legos"]
},
{
"linkToArticle": "/article2",
"keywords": ["houses"]
},
{
"linkToArticle": "/article3",
"keywords": ["legos"]
}
]下面是我的article1视图和推荐其他文章的实现:
app.get("/article1", (req, res) => {
let rawData = fs.readFileSync('./articles.json'); // get contents from articles.json
let data = JSON.parse(rawData); // parse content to JSON
let route = req.originalUrl; // get the route "/article1"
let keywords;
// gets the keyword associated with current article
for (let i = 0; i < data.length; ++i) {
if (route === data[i]["linkToArticle"]) {
keywords = data[i]["keywords"];
}
}
// check what other articles have that same keyword
// and pass to ejs
let linksToRelatedArticles = [];
for (let j = 0; j < data.length; ++j) {
if (keywords.includes(data[j]["keywords"][0])) {
linksToRelatedArticles.push(data[j]["linkToArticle"]);
}
}
res.render("article1.ejs", { "links": linksToRelatedArticles });
})我的问题是:
发布于 2022-01-10 04:07:13
内容推荐可以以多种方式完成,其复杂性有很大的差异。使用关键字是一个很好的简单解决方案,以后可以很容易地重新处理,因为您可以获得更多关于这些文章的数据以供推荐。
更有效的方法是使用数据库,这样可以更快地查询和检索这些文章。
https://stackoverflow.com/questions/70647658
复制相似问题