Open In App

Express.js req.accepts() Function

Last Updated : 17 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The req.accepts() function checks if the specified content types are acceptable on the basis of the requests Accept HTTP header field. The method returns the best match, else it returns false if none of the specified content types is acceptable. 

Syntax:

req.accepts( types )

Parameter: The type value is a single MIME type string. 

Return Value: String 

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:

Example 1: 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('application/json'));
    res.end();
});
 
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});


Steps to run the program:

Make sure you have installed the express module using the following command:

npm install express

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/ with the header set to ‘ Accept: application/json’, then you will see the following output on your console:

Server listening on PORT 3000
application/json
application/json

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/ with the header set to ‘ Accept: application/json’, then you will see the following output on your console:

Output:

Server listening on PORT 3000
application/json
false

Reference: https://expressjs.com/en/5x/api.html#req.accepts



Previous Article
Next Article

Similar Reads

Difference between req.cookies and req.signedCookies in Express.js
req.cookies: Request. Cookies are supposed to be cookies that come from the client (browser) and Response. Cookies are cookies that will send back to the client (browser). Cookies are small files/data that are sent to the client with a server request and stored on the client side. This helps us to keep track of the user’s actions. Cookie-parser is
3 min read
Difference between req.query and req.params in Express
In this article, we will learn about the req.query and req.params and we will also learn the difference between them. Table of Content req.query: Dealing with URL Stuffreq.params: Figuring Out Route ThingsDifference between req.query and req.params in Express.jsreq.query: Dealing with URL Stuff:Where the Data Comes From: It's from the extra bits at
3 min read
Express.js req.acceptsEncodings() Function
The req.acceptsEncodings() function returns the first accepted encoding of the specified encodings on the basis of the request Accept-Encoding HTTP header field and it returns false if none of the specified encodings is accepted. Syntax: req.acceptsEncodings(encoding [, ...]) Parameters: The encoding parameter is the specified encoding like 'compre
2 min read
Express.js req.acceptsLanguages() Function
The req.acceptsLanguages() function returns the first accepted language of the specified language on the basis of the request that Accept-Language HTTP header field and it returns false if none of the specified languages is accepted. Syntax: req.acceptsLanguages(lang [, ...]) Parameters: The lang parameter is the language code like 'en-US', etc. Re
2 min read
Express.js req.acceptsCharsets() Function
The req.acceptsCharsets() function returns the first accepted charset of the specified character sets on the basis of the request’s Accept-Charset HTTP header field otherwise it returns false if none of the specified charsets is accepted. Syntax: req.acceptsCharsets(charset [, ...]) Parameters: The charset parameter is the charset code like 'UTF-8'
2 min read
Express.js req.param() Function
The req.param() function basically returns the value of the param name when present. When parameters are passed, then it can be accessed using this function. Syntax: req.param(name [, defaultValue]) Parameter: The name parameter is the name of the parameter. Return Value: String. Installation of the express module: You can visit the link to Install
2 min read
Express.js req.is() Function
The req.is() function returns the matching content-type if the incoming request’s 'Content-Type' HTTP header field matches with the MIME type that has been specified by the type parameter and it returns null if the request has no body otherwise it returns false. Syntax: req.is( type ) Parameter: The type parameter is the MIME type specified. Return
2 min read
Express.js req.get() Function
The req.get() function returns the specified HTTP request header field which is a case-insensitive match and the Referrer and Referrer fields are interchangeable. Syntax: req.get( field ) Parameter: The field parameter specifies the HTTP request header field. Return Value: String. Installation of the express module: You can visit the link to Instal
2 min read
Express.js req.range() Function
The req.range() function is basically a Range header parser where the Accept-Ranges response-header field allows the server to indicate its acceptance of range requests for a resource. Syntax: req.range(size[, options]) Parameter: size: It is the maximum size of the resource.options: It is an object which can have combine property. Return Value: It
2 min read
Express.js req.subdomains Property
The req.subdomains property contains an array of subdomains in the domain name of the request. The application property subdomain offset, which defaults to 2 determines the beginning of the subdomain segments. Syntax: req.subdomains Parameter: No parameters. Returns: Array Installation of the express module: You can visit the link to Install the ex
2 min read