Thursday 7 March 2019

Express: req.acceptsLanguages(lang [, ...]): Return the accepted language by client


req.acceptsLanguages(lang [, ...])
Returns the first accepted language of the specified languages, based on the request’s Accept-Language HTTP header field. If none of the specified languages is accepted, returns false.

Example
req.acceptsLanguages('fr-CH','de', 'en', 'en_US')

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

const app = express()

app.use(cookieParser()); 

app.get('/', (req, res) => {
 var temp = req.acceptsLanguages('fr-CH','de', 'en', 'en_US')
 
 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 can see below kind of response.



Previous                                                 Next                                                 Home

No comments:

Post a Comment