Express.js req.accepts() Function

Last Updated : 31 Aug, 2026

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. 

frame_3282

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 express

After installing the express module, you can check your express version in the command prompt using the command.

npm version express

After 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.js

Project structure:

NodeProj

Example 1: Filename: index.js 

javascript
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.js

Output:

Console Output:

Server listening on PORT 3000

Browser Output:

Now make a GET request to http://localhost:3000/ then you will see the following output on your console:

Screenshot-2026-08-12-164102

Example 2: Filename: index.js 

javascript
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.js

Now make a GET request to http://localhost:3000/ , then you will see the following output on your console:

Output:

Screenshot-2026-08-12-164433

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

Comment