req.acceptsCharsets() checks whether a given character set is acceptable to the client,
based on the Accept-Charset header. It returns the first match, or false.
Note:
Accept-Charsetis largely obsolete now that UTF-8 is universal, but the method remains available for legacy or specific use cases.
Syntax
req.acceptsCharsets(charset)
Example
const express = require("express");
const app = express();
const PORT = 4700;
app.get("/legacy-export", (req, res) => {
const charset = req.acceptsCharsets("ISO-8859-1", "UTF-8");
res.send(`Using charset: ${charset}`);
});
app.listen(PORT, () => console.log(`Listening on ${PORT}`));
Use cases
- Supporting older clients with specific character-encoding requirements.
- Negotiating character sets for exported files (e.g. CSV downloads).
- Maintaining compatibility with legacy systems.