express-minify-html is an Express middleware designed to automatically minify HTML responses generated by `res.render` calls. It leverages the robust `html-minifier` library under the hood, passing configuration options directly to it. The current stable version is 0.12.0. While there's no stated release cadence, its versioning suggests a pre-1.0 status, implying potential API changes in minor versions. A key differentiator is its seamless integration with Express's existing `res.render` function, with an option to `override` it directly or expose a new `res.renderMin` method. It also supports `exception_url` configurations, allowing developers to selectively bypass minification for specific routes using strings, regular expressions, or custom functions. This helps optimize performance by reducing HTML payload size for most requests without affecting critical or dynamic paths that might be sensitive to minification.
npm install express-minify-htmlVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to apply the `express-minify-html` middleware globally to an Express application, showing its effect on HTML rendered via `res.render`.
Either set `override: true` to have `res.render` automatically minify, or ensure all calls needing minification are updated to `res.renderMin` when `override` is `false`.
Carefully consult the official `html-minifier` documentation for recommended and safe configuration options. Start with basic options like `collapseWhitespace` and incrementally add others.
Use `const minifyHTML = require('express-minify-html');` for consistent behavior, especially in older Node.js projects or those not configured for ESM-CJS interop.Run `npm install express-minify-html` in your project directory.
Ensure `const express = require('express');` and `const app = express();` are correctly set up before calling `app.use()`.Use `const minifyHTML = require('express-minify-html');` to correctly import the middleware factory.