The req.acceptsCharsets() function checks whether the specified character sets are acceptable based on the request's Accept-Charset HTTP header. It returns the first matching character set, or false if none of the specified character sets is acceptable.

Note: The Accept-Charset HTTP header is deprecated because UTF-8 is widely supported. The req.acceptsCharsets() method is still available in Express for compatibility and specific use cases.
Syntax:
req.acceptsCharsets(charset)Parameters: The charset parameter specifies one or more character sets to check, such as utf-8 or iso-8859-1.
Return Value: Returns the first accepted character set as a string, or false if none of the specified character sets 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 expressAfter installing the express module, you can check your express version in the command prompt using the command.
npm version expressAfter 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.jsProject Structure:

Example 1: Filename: index.js
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
const charset = req.acceptsCharsets('UTF-8');
console.log(charset);
res.send(`Accepted charset: ${charset}`);
});
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.jsOutput:
Console Output:
Server listening on PORT 3000Browser Output:
Now make a GET request to http://localhost:3000/ with the header set to ' Accept-Charset: UTF-8', then you will see the following output on your browser:

Console Output:

Example 2: Checking Multiple Character Sets
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
const charset = req.acceptsCharsets(
'ISO-8859-1',
'UTF-8'
);
console.log(charset);
res.send(`Accepted charset: ${charset}`);
});
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.jsNow make a GET request to http://localhost:3000/ with the header set to ' Accept-Charset: UTF-8', then you will see the following output on your console:
Output:

Working of req.acceptsCharsets()
- The client sends an HTTP request with an Accept-Charset header.
- Express reads the character sets specified in the header.
- req.acceptsCharsets() compares the requested character sets with the values provided to the method.
- Express returns the first matching character set.
- If none of the specified character sets is acceptable, it returns false.
Use Cases of req.acceptsCharsets()
- Checking whether a client accepts a specific character set.
- Supporting legacy applications that use different character encodings.
- Implementing character-set negotiation in specific HTTP applications.
- Handling requests that explicitly provide Accept-Charset preferences.
- Supporting compatibility with older clients or systems.
Reference: https://expressjs.com/en/5x/api.html#req.acceptsCharsets