devnotes.

req.acceptsEncodings() Function

req.acceptsEncodings() checks whether a given content encoding (like gzip or br) is acceptable to the client, based on the Accept-Encoding header.

Syntax

req.acceptsEncodings(encoding)

Example

const express = require("express");
const app = express();
const PORT = 4700;

app.get("/large-dataset", (req, res) => {
    const encoding = req.acceptsEncodings(["br", "gzip", "deflate"]);
    res.send(`Will compress using: ${encoding}`);
});

app.listen(PORT, () => console.log(`Listening on ${PORT}`));

Use cases

  • Choosing a compression format for large responses.
  • Optimizing bandwidth usage for data-heavy endpoints.
  • Supporting multiple compression schemes gracefully.
req.acceptsEncodings() Function | DevNotes