nestjs: express html 压缩

压缩 HTML,为nodejs express 服务器
更新于: 2024-03-09 11:08:49

安装

yarn add express-minify-html express

配置

export const EXPRESS_MINIFY_HTML_OPTIONS = {
  override: true,
  exception_url: false,
  htmlMinifier: {
    removeComments: true,
    collapseWhitespace: true,
    collapseBooleanAttributes: true,
    removeAttributeQuotes: true,
    removeEmptyAttributes: true,
    minifyJS: true
  }
};

使用

var express    = require('express');
var minifyHTML = require('express-minify-html');

var app = express();

app.use(minifyHTML({
    override:      true,
    exception_url: false,
    htmlMinifier: {
        removeComments:            true,
        collapseWhitespace:        true,
        collapseBooleanAttributes: true,
        removeAttributeQuotes:     true,
        removeEmptyAttributes:     true,
        minifyJS:                  true
    }
}));

app.get('hello', function (req, res, next) {
    res.render('helloTemplate', { hello : 'world'}, function(err, html) {
        // The output is minified, huzzah!
        console.log(html);
        res.send(html);
    })
});

参考