How to enable Brotli when using Express

Before providing introductions on how to enable Brotli for your Express app, please be aware that it would be better to let either your reverse proxy/server (like NGINX or Apache) or your platform (e.g. Cloudflare) handle Brotli for you. Only if not possible otherwise, you should resort to the instructions.

Using shrink-ray-current

The very popular compression middleware doesn't support Brotli. As a replacement, the shrink-ray-current middleware can be used, supporting GZIP, Zopfli and also Brotli, serving the best possible compression to the agent. Node.js 11.7.0 or higher is necessary though:

const shrinkRay = require('shrink-ray-current');
const express = require('express');
const app = express();
app.use(shrinkRay());
// Further routes
app.listen(3000, () => {
console.log('Server listening on port 3000');
});