【发布时间】:2022-01-20 05:34:06
【问题描述】:
假设我们有一个 JSON:
[
{
"id": 1,
"title": "Title 1",
"description": "Description 1"
},
{
"id": 2,
"title": "Title 2",
"description": "Description 2"
},
{
"id": 3,
"title": "Title 3",
"description": "Description 3"
}
]
还有一个名为 index.ejs 的 EJS 文件
<!DOCTYPE html>
<html lang="en">
<body>
<% data.forEach(item => { %>
<h1><%- item.title %></h1>
<p><%- item.description %></p>
<% }) %>
</body>
</html>
我可以简单地使用这个 javascript 代码通过 express 呈现数据:
const express = require("express");
const app = express();
const data = require("./data.json");
app.set("view engine", "ejs");
app.get("/", (req, res) => {
res.render("index", { data });
});
app.listen(4000);
但是,如果我想创建 3 个 HTML 页面并通过 Nginx 提供它们怎么办?
我可以使用 fs 创建 html 文件。但问题是,如何使用 EJS 创建 3 个生成的页面,但通过 Nginx 而不是 node.js 的 app.get() 呈现页面?
【问题讨论】:
标签: javascript node.js express nginx fs