【发布时间】:2017-07-28 09:07:41
【问题描述】:
我有许多产品,并希望为每个产品动态创建一个路由,因此当我将产品添加到 CMS 时,我正在执行 API 调用以获取 JSON,它会自动创建页面而不是手动编码.
快递:
var scopedEntries;
//get contentful entries
client.getEntries() //get entries
.then(function (entries){
scopedEntries = entries; //assigning globally to later pass into routes
console.log(entries.items[0].fields.companyName); //log data
}); // end of promise
router.get('/test:id', function (req, res) {
//console.log(scopedEntries);
res.render('test' + req.params.id, {
entries: scopedEntries
});
});
模板示例:
<html>
<head>
<title>TEST APP</title>
</head>
<body>
<h1><%= entries.items[0].fields.companyName %></h1>
<h2><%= entries.items[1].fields.productName %></h2>
<p>Description: <%= entries.items[1].fields.productDescription %></p>
<p>Size: <%= entries.items[1].fields.sizetypecolor %></p>
<p>Price: <%= entries.items[1].fields.price %></p>
<p>SKU: <%= entries.items[1].fields.productDescription %></p>
<img src="<%= entries.includes.Asset[0].fields.file.url %>" alt="" />
</body>
</html>
因此,理想情况下,JSON 响应中的每个项目都有一个页面,并且它是在快速路由中使用 EJS 动态创建的。我将如何正确地做到这一点。
另外,JSON 不是最终的 JSON。该架构尚未完全播种,因此我需要将其调整为我的树。
【问题讨论】:
-
您可能不想为每个产品都创建一条路线。可能,您想创建一个处理所有产品的单个路由,然后在您的路由中指定一个参数(路由的一部分或查询参数),告诉单个路由处理程序应该显示哪个产品。
-
感谢您的评论!具体我该怎么做?
-
要执行我上面的建议,我们必须了解您网站的整体 URL 设计,以便我们可以准确地建议如何将产品参数放入 URL 结构中。
-
我可以将 IDS 作为产品名称
-
想象一下,如果测试路线是 /product/:ID