Express.js req.acceptsEncodings() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

In Express.js, req.acceptsEncodings() checks the request’s Accept-Encoding header and returns the first encoding you offered that the client accepts — for example 'gzip' or 'br'. If none match, it returns false.

It only negotiates the name. You (or compression middleware) must still compress the body and set Content-Encoding. Unlike Accept-Charset, browsers still send Accept-Encoding routinely.

Syntax

Call req.acceptsEncodings in your Express.js app. Start with this line.

Express
req.acceptsEncodings(encoding)
req.acceptsEncodings(encoding1, encoding2, ...)
req.acceptsEncodings([encoding1, encoding2, ...])
CallWhat it does
req.acceptsEncodings(encoding)Primary signature.
req.acceptsEncodings(encoding1, encoding2, ...)Related call form.
req.acceptsEncodings([encoding1, encoding2, ...])Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

req.acceptsEncodings(encoding)
req.acceptsEncodings(encoding1, encoding2, ...)
req.acceptsEncodings([encoding1, encoding2, ...])

Parameters

ParameterDescription
encoding(s)Required. One encoding, several arguments, or an array — for example 'gzip', 'deflate', 'br', 'identity'.

Return Value

req.acceptsEncodings(...) returns a matching encoding string, or false when none of the listed encodings are acceptable.

Express
if (req.acceptsEncodings('gzip')) {
  // compress and set Content-Encoding: gzip
}

Express.js Examples

Example 1 — Pick gzip from Accept-Encoding

Offer gzip and deflate; Express returns the first accepted encoding from your list.

Express
const express = require('express');
const app = express();

app.get('/report', (req, res) => {
  // Accept-Encoding: gzip, deflate
  const encoding = req.acceptsEncodings('gzip', 'deflate');
  // => 'gzip'

  res.json({ encoding: encoding });
});

app.listen(3000);

How It Works

  1. List encodings you support. Both arguments are candidates.
  2. Read Accept-Encoding. The client advertises gzip and deflate.
  3. First accepted wins. Official docs show this case returning 'gzip'.
  4. Next step is yours. A real app would compress with gzip and set Content-Encoding: gzip.

Example 2 — Unsupported Encoding Returns false

If you only offer br but the client sent gzip, deflate, negotiation fails.

Express
const express = require('express');
const app = express();

app.get('/payload', (req, res) => {
  // Accept-Encoding: gzip, deflate
  const encoding = req.acceptsEncodings('br');
  // => false

  res.json({ encoding: encoding });
});

app.listen(3000);

How It Works

  1. Offer only Brotli. 'br' is not in the client header for this sample.
  2. Result is false. Matches the Express documentation example.
  3. Do not invent compression. Falling back to identity (no compression) is the usual next step.
  4. Offer what you implement. Include gzip if most browsers send it.

Example 3 — Fall Back to Uncompressed (identity)

Prefer sending plain bytes when no offered compression matches.

Express
const express = require('express');
const app = express();

app.get('/data', (req, res) => {
  const encoding = req.acceptsEncodings(['br', 'gzip']) || 'identity';
  const body = JSON.stringify({ ok: true });

  if (encoding === 'identity') {
    return res.type('json').send(body);
  }

  // In production: compress `body` with `encoding`, then:
  res.set('Content-Encoding', encoding);
  res.type('json').send(body); // placeholder — send compressed bytes here
});

app.listen(3000);

How It Works

  1. Pass an array. Same idea as multiple arguments.
  2. Use || 'identity'. Covers false with an uncompressed response.
  3. Set the header only when compressed. Uncompressed responses omit Content-Encoding or use identity semantics.
  4. Compression is separate. Libraries such as compression middleware automate this pipeline for many apps.

Runtime Support

req.acceptsEncodings() is an Express.js request method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only send Accept-Encoding.

✓ Server · Express 4 & 5

req.acceptsEncodings()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
req.acceptsEncodings() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It checks the Accept-Encoding header and returns the first encoding you offered that the client accepts (for example gzip). If none match, it returns false.
The singular alias req.acceptsEncoding() is deprecated since Express 4.5.0. Use the plural req.acceptsEncodings() instead.
Usually send the body uncompressed (identity). Prefer that over 406 for encoding negotiation in typical APIs.
No. It only chooses an encoding name. You still need compression code or middleware to actually gzip or brotli the bytes and set Content-Encoding.
No. It runs on your Node.js server. Browsers only send Accept-Encoding on the request.

Next: req.acceptsLanguages()

Negotiate preferred languages from the Accept-Language header.

req.acceptsLanguages() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful