The req.accepts() function checks whether the specified content types are acceptable based on the request's Accept HTTP header. It returns the best matching content type, or false if none of the specified types is acceptable.Â

Syntax:
req.accepts( types )Parameter: The types parameter specifies one or more content types to check. It can be a MIME type, extension name, comma-delimited list, or array.
Return Value: Returns the best matching content type as a string, or false if none of the specified types is acceptable.Â
Installation of the express module:
You can visit the link to Install the express module. You can install this package by using this command.
npm install expressAfter installing the express module, you can check your express version in the command prompt using the command.
npm version expressAfter that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.
node index.jsProject structure:

Example 1: Filename: index.jsÂ
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
const type = req.accepts('application/json');
console.log(type);
res.send(`Accepted type: ${type}`);
});
app.listen(PORT, () => {
console.log(`Server listening on PORT ${PORT}`);
});
Steps to run the program:
Run the index.js file using the below command:
node index.jsOutput:
Console Output:
Server listening on PORT 3000Browser Output:
Now make a GET request to http://localhost:3000/ then you will see the following output on your console:

Example 2: Filename: index.jsÂ
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', function (req, res) {
console.log(req.get('Accept'));
console.log(req.accepts('text/plain'));
res.end();
});
app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Steps to run the program:
Run the index.js file using the below command:
node index.jsNow make a GET request to http://localhost:3000/ , then you will see the following output on your console:
Output:

Working of req.accepts()
- The client sends an HTTP request with an Accept header.
- Express reads the content types specified in the Accept header.
- req.accepts() compares the requested types with the types provided to the method.
- Express returns the best matching content type.
- If none of the specified types is acceptable, it returns false.
Use Cases of req.accepts()
- Checking which response format the client supports.
- Selecting between JSON, HTML, and other response formats.
- Implementing content negotiation.
- Returning appropriate responses based on client preferences.
- Handling unsupported response formats with a 406 Not Acceptable response.
Reference:Â https://expressjs.com/en/5x/api.html#req.accepts