Express req.acceptsEncodings() Method
What you’ll learn
- How
req.acceptsEncodings()negotiates compression formats. - How to choose between
br,gzip, and fallback behavior. - How this method supports response optimization strategies.
- How to avoid encoding negotiation mistakes.
Syntax
javascript
req.acceptsEncodings(encoding)
req.acceptsEncodings(encoding1, encoding2, ...)
req.acceptsEncodings([encodings])1
Choose best encoding
javascript
app.get('/content', function (req, res) {
var encoding = req.acceptsEncodings('br', 'gzip', 'identity');
if (!encoding) return res.status(406).send('Not Acceptable');
res.set('Content-Encoding', encoding);
res.send('Compressed or identity response');
});2
Safe identity fallback
javascript
app.get('/plain', function (req, res) {
var encoding = req.acceptsEncodings('gzip', 'identity') || 'identity';
res.set('Content-Encoding', encoding);
res.send('Plain fallback content');
});❓ FAQ
It checks which content encodings the client accepts and returns the best match among encodings you provide.
It returns false so you can choose fallback behavior, such as sending uncompressed output.
Yes. It is often used with compression logic to pick supported encodings like br or gzip.
Yes. You can pass multiple arguments or an array, such as req.acceptsEncodings('br', 'gzip').
Most modern clients advertise several options, commonly including gzip and sometimes br.
Did you know?
req.acceptsEncodings() checks the Accept-Encoding header and helps choose response compression like br or gzip.
4 people found this page helpful
