Express.js req.acceptsEncodings() Function

Last Updated : 31 Aug, 2026

The req.acceptsEncodings() function checks whether the specified content encodings are acceptable based on the request's Accept-Encoding HTTP header. It returns the first matching encoding, or false if none of the specified encodings is acceptable. 

frame_3284

Syntax:

req.acceptsEncodings(encoding)

Parameters: The encoding parameter specifies one or more content encodings to check, such as gzip, br, or deflate. 

Return Value: Returns the first accepted encoding as a string, or false if none of the specified encodings is acceptable.

Installation of the express module:

You can visit the link to Install the express module. You can install this package by using this command.

npm install express

After installing the express module, you can check your express version in the command prompt using the command.

npm version express

After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.

node index.js

Project Structure:

NodeProj

Example 1: Checking an Accepted Encoding

javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
    const encoding = req.acceptsEncodings('gzip');
    console.log(encoding);
    res.send(`Accepted encoding: ${encoding}`);
});
app.listen(PORT, () => {
    console.log(`Server listening on PORT ${PORT}`);
});

Steps to run the program:

Run the index.js file using the below command:

node index.js

Output:

Console Output:

Server listening on PORT 3000

Browser Output:

Now make a GET request to http://localhost:3000/ with the header set to 'Accept-Encoding: gzip', then you will see the following output on your browser:

Screenshot-2026-08-12-174108

Console Output:

Screenshot-2026-08-12-173950


Example 2: Checking Multiple Encodings

javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
    const encoding = req.acceptsEncodings(
        'br',
        'gzip',
        'deflate'
    );
    console.log(encoding);
    res.send(`Accepted encoding: ${encoding}`);
});
app.listen(PORT, () => {
    console.log(`Server listening on PORT ${PORT}`);
});

Steps to run the program:

Run the index.js file using the below command:

node index.js

Now make a GET request to http://localhost:3000/ with the header set to 'Accept-Encoding: gzip', then you will see the following output on your browser:

Output:

Screenshot-2026-08-12-173950

Working of req.acceptsEncodings()

  • The client sends an HTTP request with an Accept-Encoding header.
  • Express reads the content encodings specified in the header.
  • req.acceptsEncodings() compares the requested encodings with the values provided to the method.
  • Express returns the first matching encoding.
  • If none of the specified encodings is acceptable, it returns false.

Use Cases of req.acceptsEncodings()

  • Checking which content encodings the client supports.
  • Selecting an appropriate compression format for a response.
  • Supporting content negotiation based on client preferences.
  • Handling different encodings such as gzip, br, and deflate.
  • Optimizing response size and network performance.

Reference: https://expressjs.com/en/4x/api.html#req.acceptsEncodings

Comment