Thursday, 7 March 2019

Express: req.acceptsEncodings(encoding [, ...]): Get the accepted encodings by client


req.acceptsEncodings(encoding [, ...])
Returns the first accepted encoding of the specified encodings, based on the request’s Accept-Encoding HTTP header field.

If no encoding matches, then it returns false.

Example
var temp = req.acceptsEncodings('gzip', 'compress', 'deflate')

index.js

const express = require('express')
var cookieParser = require('cookie-parser')

const app = express()

app.use(cookieParser()); 

app.get('/', (req, res) => {
 var temp = req.acceptsEncodings('gzip', 'compress', 'deflate')
 
 if(temp){
  var responseData = ('Client accepts ' + temp)
 }

 res.send(responseData)
})

const port = 3000
app.listen(port, () => console.log(`Application started listening on port ${port}!`));

Run index.js


Open the url ‘http://localhost:3000/’ in browser, you will see below kind of response.


Previous                                                 Next                                                 Home

No comments:

Post a Comment