Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Express.js Request Complete Reference
Next article icon

Express.js req.acceptsLanguages() Function

Last Updated : 17 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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. 

Return Value: String (if true) or False. 

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-Language'));
    console.log(req.acceptsLanguages('en-US'));
    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-Language: en-US’, then you will see the following output on your console:

Server listening on PORT 3000 en-US en-US

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-Language'));
    console.log(req.acceptsLanguages('fr-CH'));
    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

Output:

Browser Output:

Now make a GET request to http://localhost:3000/ with the header set to ‘Accept-Language: en-US’, then you will see the following output on your console:

Server listening on PORT 3000 en-US false

Reference: https://expressjs.com/en/4x/api.html#req.acceptsLanguages



Next Article
Express.js Request Complete Reference
author
gouravhammad
Improve
Article Tags :
  • Express.js
  • Node.js
  • Web Technologies

Similar Reads

  • Express.js Tutorial
    Express.js is a minimal and flexible Node.js web application framework that provides a list of features for building web and mobile applications easily. It simplifies the development of server-side applications by offering an easy-to-use API for routing, middleware, and HTTP utilities. Built on Node
    5 min read
  • Top 50+ ExpressJS Interview Questions and Answers
    ExpressJS is a fast, unopinionated, and minimalist web framework for NodeJS, widely used for building scalable and efficient server-side applications. It simplifies the development of APIs and web applications by providing powerful features like middleware support, routing, and template engines. In
    15+ min read
  • Express Basics

    • Introduction to Express
      Prerequisite - Node.js What is Express? Express is a small framework that sits on top of Node.js's web server functionality to simplify its APIs and add helpful new features.It makes it easier to organize your application's functionality with middle ware and routing; it adds helpful utilities to Nod
      2 min read

    • Steps to Create an Express.js Application
      Creating an Express.js application involves several steps that guide you through setting up a basic server to handle complex routes and middleware. Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Here’s a
      10 min read

    • Print hello world using Express JS
      In this article, we will learn how to print Hello World using Express jS. Express JS is a popular web framework for Node.js. It provides various features that make it easy to create and maintain web applications. Express Js Program to Print Hello world:"Hello, World!" in Express JS serves as an exce
      2 min read

    • Build Your First Router in Node.js with Express
      Express.js is a powerful framework for node.js. One of the main advantages of this framework is defining different routes or middleware to handle the client's different incoming requests. In this article, we will discuss, how to use the router in the express.js server. The express.Router() function
      2 min read

    • Middleware in Express
      Middleware in Express refers to functions that process requests before reaching the route handlers. These functions can modify the request and response objects, end the request-response cycle, or call the next middleware function. Middleware functions are executed in the order they are defined. They
      6 min read

    Basic Express Guide

    • Routing Path for ExpressJS
      What and Why ? Routing in ExpressJS is used to subdivide and organize the web application into multiple mini-applications each having its own functionality. It provides more functionality by subdividing the web application rather than including all of the functionality on a single page. These mini-a
      3 min read

    • Explain the use of req and res objects in Express JS
      Express JS is used to build RESTful APIs with Node.js. We have a 'req' (request) object in Express JS which is used to represent the incoming HTTP request that consists of data like parameters, query strings, and also the request body. Along with this, we have 'res' (response) which is used to send
      4 min read

    • Error Handling in Express
      Error handling in Express ensures that your application runs smoothly by catching and managing errors before they cause issues. It allows developers to log errors, fix problems faster, and send clear responses to users, preventing technical error messages from appearing. What is Error Handling in Ex
      5 min read

    • How to do Templating using ExpressJS in Node.js ?
      Template Engine : A template engine basically helps us to use the static template files with minimal code. At runtime, the template engine replaces all the variables with actual values at the client-side. Templating Engine Examples: EJS (Embedded JavaScript Templating) Pug Mustache In this article w
      2 min read

    • How To Serve Static Files in ExpressJS?
      ExpressJS is a popular web framework for NodeJS that allows developers to build robust web applications. One of its core functionalities is serving static files such as images, CSS, JavaScript, and HTML files. Syntax app.use(express.static(path.join(__dirname, 'public')));Serves Static Files: This l
      2 min read

    • How to enable debugging in Express App ?
      In express, there is a module present called DEBUG that gives log information. It tells about middleware functions, application mode, their status, and also about the requests and responses made to the server. To use this module while running the express app, set the DEBUG environment variable to ex
      3 min read

    Express.js express() Methods

    • ExpressJS express.json() Function
      The express.json() function is a built-in middleware in Express that is used for parsing incoming requests with JSON payload. The express.json middleware is important for parsing incoming JSON payloads and making that data available in the req.body or further processing within the routes. Without us
      4 min read

    • Express.js express.raw() Function
      The express.raw() function is a built-in middleware function in Express. It parses incoming request payloads into a Buffer and is based on body-parser. Syntax: express.raw( [options] )Parameter: The options parameter contains various properties like inflate, limit, type, etc. Return Value: It return
      2 min read

    • Express express.Router() Function
      The express.Router() function in Express.js creates a new router object that can handle requests in a modular and organized way. It serves as a mini-application with middleware and routes but is limited to specific segments of your application. By using express.Router(), you can organize your Expres
      3 min read

    • Express express.static() Function
      In Express.js, serving static files like images, CSS, and JavaScript used to require custom routes. With the express.static() function, you can serve static content directly from a folder, making it easier and faster. Let's explore how this function works and how you can use it in your web applicati
      4 min read

    • Express.js express.text() Function
      The express.text() function is a built-in middleware in Express.js that parses incoming HTTP request bodies with a text/plain content type. It allows you to easily handle raw text data sent in the body of a request, making it suitable for handling non-JSON, non-URL-encoded, or non-multipart data. Th
      5 min read

    • Express express.urlencoded() Function
      The express.urlencoded() middleware in Express.js is used to parse URL-encoded form data, making it accessible as a JavaScript object in req.body. It's essential for handling form submissions in application/x-www-form-urlencoded format. Syntax app.use( express.urlencoded({ extended: true, inflate: t
      3 min read

    • Express.js express() function Complete Reference
      Express.js is a small framework that works on top of Node.js web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality with middleware and routing. Express.js express() Methods:Method Description Express.js express.jso
      1 min read

    Express Application Methods

    • Express app.delete() Function
      The `app.delete()` function is utilized to handle HTTP DELETE requests for a specified path. It takes the path as a parameter and also accepts callback functions as parameters to handle the request. Syntax: app.delete(path, callback)Parameters: path: It is the path for which the middleware function
      2 min read

    • Express.js | app.disable() Function
      The app.disable() function is used to set the boolean setting name to false. It is basically the shorthand for the app.set(name, false). So instead we can use app.disable(name) function to set the false boolean value to some system Express.js settings. Syntax: app.disable(name) Installation of the e
      1 min read

    • Express.js | app.disabled() Function
      The app.disabled() function is used to return the bool values of the setting name. It returns true if the setting name is disabled and returns false if the setting name is not disabled. Syntax: app.disabled(name) Installation of the express module: You can visit the link to Install the express modul
      1 min read

    • Express.js | app.enable() Function
      The app.enable() function is used to set the boolean value i.e. name to true. It is basically the shorthand for the app.set(name, true) or app.set(name, false). So instead we can use app.enable(name) function to set boolean values to some system Express.js settings. Syntax: app.enable(name) Installa
      1 min read

    • Express.js | app.enabled() Function
      The app.enabled() function is used to return the bool values of the setting name. It returns true if the setting name is enabled and returns false if the setting name is not enabled. Syntax: app.enabled(name) Installation of the express module: You can visit the link to Install the express module. Y
      1 min read

    • Express.js app.mountpath Property
      The app.mountpath property contains one or more path patterns on which a sub-app was mounted. Syntax: app.mountpath Parameter: No parameters. 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 com
      2 min read

    • Express.js Mount Event
      The mount event is fired on a sub-app when it is mounted on a parent app and the parent app is basically passed to the callback function. Syntax: app.on('mount', callback(parent)) Parameter: It is an event named 'mount', and the callback function is called when this event is called. Return Value: Si
      2 min read

    • Express.js | app.all() Function
      The app.all() function is used to route all types of HTTP requests. Like if we have POST, GET, PUT, DELETE, etc, requests made to any specific route, let's say /user, so instead of defining different APIs like app.post('/user'), app.get('/user'), etc, we can define single API app.all('/user') which
      2 min read

    • Express.js Application Complete Reference
      Express.js is a small framework that works on top of Node.js web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality with middleware and routing. Express.js Application Properties:Properties Description Express.js ap
      4 min read

    Express Request Methods

    • Express.js req.app Property
      The req.app property holds the reference to the instance of the Express application that is using the middleware. Syntax: req.app Parameter: No parameters. Return Value: Object Installation of the express module: You can visit the link to Install the express module. You can install this package by u
      2 min read

    • Express.js req.baseUrl Property
      The req.baseUrl property is the URL path on which a router instance was mounted. The req.baseUrl property is similar to the mount path property of the app object, except app.mountpath returns the matched path pattern(s). Syntax: req.baseUrlParameter: No parameters. Return Value: String Installation
      2 min read

    • Express req.body Property
      The req.body property contains key-value pairs of data submitted in the request body. By default, it is undefined and is populated when you use a middleware called body-parsing such as express.urlencoded() or express.json(). Syntax: req.bodyParameter: No parameters. Return Value: Object The req.body
      4 min read

    • Express.js req.cookies Property
      The req.cookies property is used when the user is using cookie-parser middleware. This property is an object that contains cookies sent by the request. Syntax: req.cookiesParameter: No parameters. Return Value: Object Installation of the express module: You can visit the link to Install the express
      2 min read

    • Express.js req.fresh Property
      The req.fresh property returns true if the response is still 'fresh' in the client’s cache else it will return false. Syntax: req.fresh Parameter: No parameter Return Value: True or False Installation of the express module: You can visit the link to Install the express module. You can install this p
      2 min read

    • Express.js req.accepts() Function
      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
      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
      2 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 encodin
      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 pa
      2 min read

    • Express.js Request Complete Reference
      Express.js is a small framework that works on top of Node.js web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality with middleware and routing. Express.js Request Properties:Properties Description Express.js req.ap
      4 min read

    Express Response methods

    • Express.js res.app Property
      The res.app property holds a reference to the instance of the Express application that is using the middleware. Syntax: res.app Parameter: No parameters. Return Value: Object Installation of the express module: You can visit the link to Install the express module. You can install this package by usi
      2 min read

    • Express.js res.headersSent Property
      The res.headersSent property is a boolean property that indicates if the app sent HTTP headers for the response. Syntax: res.headersSent Parameter: No parameters. Return Value: This property returns a Boolean value either True or False. Installation of the express module: You can visit the link to I
      2 min read

    • Express res.locals Property
      The `res.locals` property is an object that holds response local variables specific to the current request. It has a scope limited to the request and is accessible only to the view(s) rendered during that particular request/response cycle, if any. Syntax: res.localsParameter: No parameters. Return V
      2 min read

    • Express.js res.append() Function
      The res.append() function appends the specified value to the HTTP response header field and if the header is not already set then it creates the header with the specified value. Syntax: res.append(field [, value])Parameter: The field parameter describes the name of the field that need to be appended
      2 min read

    • Express.js res.attachment() Function
      The res.attachment() function is used to set the HTTP response Content-Disposition header field to 'attachment'. If the name of the file is given as a filename, then it sets the Content-Type based on the extension name through the res.type() function and finally sets the Content-Disposition 'filenam
      2 min read

    • Express res.cookie() Function
      The res.cookie() function is used to set a cookie in the client's browser. It allows you to assign a cookie by providing a name and a value. The value can be a simple string or an object, which will be automatically converted to JSON. Syntax: res.cookie(name, value [, options])name: The name of the
      3 min read

    • Express.js res.clearCookie() Function
      The res.clearCookie() function is used to clear the cookie specified by name. This function is called for clearing the cookies which as already been set. For example, if a user cookie is set, then it can be cleared using this function. Syntax: res.clearCookie(name, [ options ])Parameters: Name: It i
      2 min read

    • Express.js res.download() Function
      The res.download() function transfers the file at the path as an 'attachment'. Typically, browsers will prompt the user to download. Syntax: res.download(path [, filename] [, options] [, fn])Parameters: The filename is the name of the file which is to be downloaded as an attachment and fn is a callb
      2 min read

    • Express res.end() Function
      The res.end() function concludes the response process and is derived from the HTTP.ServerResponse's response.end() method in the Node core. It is employed to promptly conclude the response without including any data. Syntax: res.end([data] [, encoding])Parameters: The default encoding is 'utf8' and
      2 min read

    • Express.js Response Complete Reference
      Express.js is a small framework that works on top of Node.js web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality with middleware and routing. Express.js Response Properties:Properties Description Express.js res.a
      4 min read

    Express Router Methods

    • Express.js router.all() Function
      The router.all() function is just like the router.METHOD() methods, except that it matches all HTTP methods (verbs). It is very helpful for mapping global logic for arbitrary matches or specific path prefixes. Syntax: router.all(path, [callback, ...] callback)Parameter: The path parameter is the pat
      2 min read

    • Express.js router.METHOD() Function
      The router.METHOD() method provides the routing functionality in Express, where METHOD is one of the HTTP methods, such as GET, PUT, POST, and so on, in lowercase. Syntax: router.METHOD(path, [callback, ...] callback) Parameter: The path parameter specifies the path on the URL and callback is the fu
      2 min read

    • Express.js router.param() function
      The parameters of router.param() are a name and function. Where the name is the actual name of the parameter and the function is the callback function. Basically, the router.param() function triggers the callback function whenever the user routes to the parameter. This callback function will be call
      2 min read

    • Express.js router.route() Function
      The router.route() function returns an instance of a single route that you can then use to handle HTTP verbs with optional middleware. You can also use the router.route() function to avoid duplicate route naming as well as typing errors. Syntax: router.route( path )Parameter: The path parameter hold
      2 min read

    • Express.js | router.use() Function
      The router.use() function uses the specified middleware function or functions. It basically mounts middleware for the routes which are being served by the specific router. Syntax: router.use( path, function )Parameters: Path: It is the path to this middleware, if we can have /user, now this middlewa
      2 min read

    • Express.js Router Complete Reference
      Express.js is a small framework that works on top of Node.js web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality with middleware and routing. Express.js Router Methods: Methods Description Express.js router.all()
      1 min read

    Express Middleware

    • How to create custom middleware in express ?
      Express.js is the most powerful framework of the node.js. Express.js is a routing and Middleware framework for handling the different routing of the webpage, and it works between the request and response cycle. Express.js use different kinds of middleware functions in order to complete the different
      2 min read

    • What is middleware chaining in Express JS, and how is it useful?
      In Express JS there is the process of Middleware chaining by which multiple middleware functions sequentially can be processed and also can be able to modify the incoming requests before they reach the final route handler. In this article, we will see the concept of Middleware Chaining in Express JS
      4 min read

    • What is express-session middleware in Express?
      In the Express web application, the express-session middleware is mainly used for managing the sessions for the user-specific data. In this article, we will see the use of express-session middleware for session management in Express with practical implementation. PrerequisitesNode JSExpress JSTable
      2 min read

    • How to use third-party middleware in Express JS?
      Express JS is a robust web application framework of Node JS which has the capabilities to build web and mobile applications. Middleware is the integral part of the framework. By using the third party middleware we can add additional features in our application. PrerequisitesNode JSExpress JSPostmanT
      2 min read

    • Difference between app-level and route-level middleware in Express
      Middleware functions are functions that have access to the request object ( req ), the response object ( res ), and the next middleware function in the application's request-response cycle. In this article, we will go through app-level and route-level middleware and also see the key differences betw
      3 min read

    • What is express-session middleware in Express?
      In the Express web application, the express-session middleware is mainly used for managing the sessions for the user-specific data. In this article, we will see the use of express-session middleware for session management in Express with practical implementation. PrerequisitesNode JSExpress JSTable
      2 min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences