如何从 ExpressJS 路由中保存呈现的 html 视图文件
How to save rendered html view files from ExpressJS Routes
我使用 ExpressJS
和 PUG
构建了几个静态网站页面以利用模板引擎。
但现在我需要导出所有 ExpressJS
Routes
.
正在渲染的所有原始 HTML
有什么软件包可以帮助我做到这一点吗?或者我必须编写自定义命令并遍历所有 Routes
并保存呈现的输出?
如果自定义命令是唯一的方法,我如何遍历所有 routes
并获得呈现的输出?
我找不到任何库或资源来实现我想要的。但是通过我的一些脏代码、技巧和包,我能够导出所有路由。
注意: 我没有编写导出 html 的节点命令,而是添加了一个路由来触发操作,这里是路由的代码:
app.use('/export_templates', router.get('/', async function (req, res, next) {
const endpoints = listEndpoints(app);
const failedEndpoints = [];
for (const i in endpoints) {
const endpoint = endpoints[i];
if (endpoint.path == '/export_templates') {
continue;
}
try {
const res = await axios.get('http://'+req.headers.host+''+endpoint.path+'?export=true');
}
catch(error) {
failedEndpoints.push(endpoint.path);
}
}
res.json({
"status": "succes",
"message": "Please check templates folder for the latest exported html templates",
"failed": failedEndpoints
})
}));
基本上这个路由会迭代并使用 export=true
参数向所有可用路由发出请求。
然后在每个路由视图函数中,条件检查导出参数是否可用,然后使用 pug
模板位置和新文件名作为函数参数调用 exportTemplateFile
函数。
如果请求不包含 export
参数请求的路由将简单地输出什么模板。
示例路线:
router.get('/', function(req, res, next) {
if (req.query.export) {
exportTemplateFile('views/index.pug', 'index.html');
}
res.render('index.pug');
});
这里是 2 util 函数完成导出过程的代码
function createTemplateFile(filename) {
fs.open(filename,'r',function(err, fd){
if (err) {
fs.writeFile(filename, '', function(err) {
if(err) {
console.log(err);
}
});
}
});
}
function exportTemplateFile(templateLocation, templateName) {
const html = pretty(pug.renderFile(templateLocation));
createTemplateFile('templates/'+templateName);
var stream = fs.createWriteStream('templates/'+templateName);
stream.once('open', function (fd) {
stream.write(html);
stream.end();
});
}
如果文件不存在,createTemplateFile
函数只是创建一个新文件。
exportTemplateFile
函数将HTML保存在pug
渲染的html
变量中,用pretty
包美化后覆盖新的模板文件。
注意: 在我的例子中,所有 pug
模板都是静态的,因此我不必将任何上下文传递给 pug.renderFile
函数。但是,如果您需要在 pug 模板中使用任何上下文,您只需将其与模板位置一起传递即可。
我使用 ExpressJS
和 PUG
构建了几个静态网站页面以利用模板引擎。
但现在我需要导出所有 ExpressJS
Routes
.
有什么软件包可以帮助我做到这一点吗?或者我必须编写自定义命令并遍历所有 Routes
并保存呈现的输出?
如果自定义命令是唯一的方法,我如何遍历所有 routes
并获得呈现的输出?
我找不到任何库或资源来实现我想要的。但是通过我的一些脏代码、技巧和包,我能够导出所有路由。
注意: 我没有编写导出 html 的节点命令,而是添加了一个路由来触发操作,这里是路由的代码:
app.use('/export_templates', router.get('/', async function (req, res, next) {
const endpoints = listEndpoints(app);
const failedEndpoints = [];
for (const i in endpoints) {
const endpoint = endpoints[i];
if (endpoint.path == '/export_templates') {
continue;
}
try {
const res = await axios.get('http://'+req.headers.host+''+endpoint.path+'?export=true');
}
catch(error) {
failedEndpoints.push(endpoint.path);
}
}
res.json({
"status": "succes",
"message": "Please check templates folder for the latest exported html templates",
"failed": failedEndpoints
})
}));
基本上这个路由会迭代并使用 export=true
参数向所有可用路由发出请求。
然后在每个路由视图函数中,条件检查导出参数是否可用,然后使用 pug
模板位置和新文件名作为函数参数调用 exportTemplateFile
函数。
如果请求不包含 export
参数请求的路由将简单地输出什么模板。
示例路线:
router.get('/', function(req, res, next) {
if (req.query.export) {
exportTemplateFile('views/index.pug', 'index.html');
}
res.render('index.pug');
});
这里是 2 util 函数完成导出过程的代码
function createTemplateFile(filename) {
fs.open(filename,'r',function(err, fd){
if (err) {
fs.writeFile(filename, '', function(err) {
if(err) {
console.log(err);
}
});
}
});
}
function exportTemplateFile(templateLocation, templateName) {
const html = pretty(pug.renderFile(templateLocation));
createTemplateFile('templates/'+templateName);
var stream = fs.createWriteStream('templates/'+templateName);
stream.once('open', function (fd) {
stream.write(html);
stream.end();
});
}
如果文件不存在,createTemplateFile
函数只是创建一个新文件。
exportTemplateFile
函数将HTML保存在pug
渲染的html
变量中,用pretty
包美化后覆盖新的模板文件。
注意: 在我的例子中,所有 pug
模板都是静态的,因此我不必将任何上下文传递给 pug.renderFile
函数。但是,如果您需要在 pug 模板中使用任何上下文,您只需将其与模板位置一起传递即可。