Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Express.js – req.acceptsCharsets() Method
The req.acceptsCharsets() method returns the first accepted charset of the specified charset sets. These charsets are based on the request's Accept-Charset HTTP header field. By default, it returns 'false' if none of the specified charsets is accepted.
Syntax
req.acceptsCharsets ( charset, [...] )
Example 1
Create a file with the name "reqAcceptsCharsets.js" and copy the following code snippet. After creating the file, use the command "node reqAcceptsCharsets.js" to run this code as shown in the example below −
// res.acceptsCharsets(lang, [...]) Method Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint
app.get('/api', function (req, res) {
console.log(req.acceptsCharsets('UTF-8'));
console.log("Charset Received: ", req.get('Accept-Charset'));
res.end();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Output
Hit the following Endpoint with a GET request "localhost:3000/api" and set the following property in header: Accept-Charsets = UTF-8.
C:\home\node>> node reqAcceptsCharsets.js Server listening on PORT 3000 Charset Received: UTF-8 UTF-8
Example 2
Let's take a look at one more example.
// res.acceptsCharsets(lang, [...]) Method Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint
app.get('/api', function (req, res) {
console.log("Is valid charset: ", req.acceptsCharsets('ISO-8859-1'));
console.log("Charset Received: ", req.get('Accept-Charset'));
res.end();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Output
Hit the Endpoint "localhost:3000/api" with a GET request and set the following property in headers - Accept-Language = UTF-8
C:\home\node>> node reqAcceptsCharsets.js Server listening on PORT 3000 Is valid charset: false Charset Received: UTF-8
