
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
express-compress-html
Advanced tools
Fast HTML minification middleware for Express using Rust-based @minify-html/node
⚡ Fast HTML minification middleware for Express using Rust-based @minify-html/node
npm install express-compress-html
const express = require('express');
const htmlMinifier = require('express-compress-html');
const app = express();
// Set your template engine
app.set('view engine', 'ejs');
// Use the middleware
app.use(htmlMinifier());
app.get('/', (req, res) => {
res.render('index');
});
app.listen(3000);
import express from 'express';
import htmlMinifier from 'express-compress-html';
const app = express();
app.set('view engine', 'ejs');
// Use with default options
app.use(htmlMinifier());
// Or with custom options
app.use(htmlMinifier({
enabled: true,
minifyOptions: {
keep_comments: false,
minify_css: true,
minify_js: true
}
}));
app.get('/', (req, res) => {
res.render('index');
});
app.listen(3000);
The middleware intercepts res.render() calls and minifies the rendered HTML before sending it to the client. This works seamlessly with:
htmlMinifier(options?)Creates an Express middleware function that minifies HTML responses.
enabled (boolean, optional)Explicitly enable or disable minification. If not set, minification is automatically enabled in production (NODE_ENV=production) or when MINIFY_HTML=true.
app.use(htmlMinifier({
enabled: true // Always minify, regardless of NODE_ENV
}));
minifyOptions (object, optional)Customize the minification behavior. All options are passed directly to @minify-html/node.
app.use(htmlMinifier({
minifyOptions: {
do_not_minify_doctype: false,
ensure_spec_compliant_unquoted_attribute_values: false,
keep_closing_tags: false,
keep_html_and_head_opening_tags: false,
keep_spaces_between_attributes: false,
keep_comments: false,
minify_css: true,
minify_js: true,
remove_bangs: false,
remove_processing_instructions: false
}
}));
Default Options:
| Option | Default | Description |
|---|---|---|
do_not_minify_doctype | false | Don't minify DOCTYPE declarations |
ensure_spec_compliant_unquoted_attribute_values | false | Ensure spec-compliant unquoted attribute values |
keep_closing_tags | false | Keep closing tags (e.g., </div>, </span>) |
keep_html_and_head_opening_tags | false | Keep HTML and HEAD opening tags |
keep_spaces_between_attributes | false | Keep spaces between attributes |
keep_comments | false | Keep HTML comments |
minify_css | true | Minify CSS in <style> tags and style attributes |
minify_js | true | Minify JavaScript in <script> tags |
remove_bangs | false | Remove bangs (e.g., <!--[if IE]>) |
remove_processing_instructions | false | Remove processing instructions (e.g., <?xml?>) |
onError (function, optional)Custom error handler for minification errors. By default, errors are logged to console.error and the original HTML is sent.
app.use(htmlMinifier({
onError: (error) => {
console.log('Minification failed:', error.message);
// Custom error handling logic
}
}));
NODE_ENVWhen NODE_ENV=production, minification is automatically enabled.
NODE_ENV=production node app.js
MINIFY_HTMLExplicitly enable minification in any environment:
MINIFY_HTML=true node app.js
When HTML is successfully minified, the middleware adds the following header:
X-HTML-Minified: true
This can be useful for debugging or monitoring.
Using the Rust-based @minify-html/node, this middleware offers:
This middleware works perfectly with express-ejs-layouts:
const express = require('express');
const expressLayouts = require('express-ejs-layouts');
const htmlMinifier = require('express-compress-html');
const app = express();
app.set('view engine', 'ejs');
app.use(expressLayouts);
app.use(htmlMinifier()); // Add after express-ejs-layouts
app.get('/', (req, res) => {
res.render('index');
});
The middleware is designed to be fault-tolerant:
const express = require('express');
const htmlMinifier = require('express-compress-html');
const app = express();
app.set('view engine', 'ejs');
app.use(htmlMinifier());
app.get('/', (req, res) => {
res.render('home');
});
app.listen(3000);
app.use(htmlMinifier({
enabled: process.env.NODE_ENV === 'production'
}));
app.use(htmlMinifier({
minifyOptions: {
keep_comments: process.env.NODE_ENV !== 'production'
}
}));
app.use(htmlMinifier({
onError: (error) => {
logger.error('HTML minification failed', {
message: error.message,
stack: error.stack
});
}
}));
Full TypeScript definitions are included:
import htmlMinifier, {
MinifyOptions,
HtmlMinifierMiddlewareOptions
} from 'express-compress-html';
const options: HtmlMinifierMiddlewareOptions = {
enabled: true,
minifyOptions: {
minify_css: true,
minify_js: true
}
};
app.use(htmlMinifier(options));
MIT
Contributions are welcome! Please feel free to submit a Pull Request.
Built with @minify-html/node - the fastest HTML minifier available for Node.js.
FAQs
Fast HTML minification middleware for Express using Rust-based @minify-html/node
We found that express-compress-html demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.