【问题标题】:How to compress EJS output in expressJS framework?如何在 expressJS 框架中压缩 EJS 输出?
【发布时间】:2022-01-15 23:39:46
【问题描述】:
我是NodeJS, Express 和模板
的新手
我正在尝试生成一个表并将输出发送给用户并且它正在工作
使用EJS 很好,但生成的 html 有很多空格。
所以当使用在线html压缩器后生成的html文件大小在148kb左右时,大小变为38kb
那么我如何在渲染后压缩html并将其发送给用户。
express 或 ejs 中是否有内置方法
我知道 pug 已经生成了压缩的 html 但我想使用EJS
【问题讨论】:
标签:
html
node.js
express
templates
ejs
【解决方案1】:
没有内置方法,试试express-minify-html
const express = require('express');
const minifyHTML = require('express-minify-html');
const app = express();
app.use(minifyHTML({
override: true,
exception_url: false,
htmlMinifier: {
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeEmptyAttributes: true,
minifyJS: true
}
}));