Express.js req.acceptsLanguages() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

In Express.js, req.acceptsLanguages() checks the request’s Accept-Language header and returns the best language your app offers. Pass one or more language tags; you get the preferred match, or false if none fit.

Call it with no arguments to get every language from the header as an array (sorted by client preference). It runs on the server via the request object — browsers only send the header.

Syntax

Call req.acceptsLanguages in your Express.js app. Start with this line.

Express
req.acceptsLanguages()
req.acceptsLanguages(lang)
req.acceptsLanguages(lang1, lang2, ...)
req.acceptsLanguages([lang1, lang2, ...])
CallWhat it does
req.acceptsLanguages()Primary signature.
req.acceptsLanguages(lang)Related call form.
req.acceptsLanguages(lang1, lang2, ...)Related call form.
req.acceptsLanguages([lang1, lang2, ...])Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

req.acceptsLanguages()
req.acceptsLanguages(lang)
req.acceptsLanguages(lang1, lang2, ...)
req.acceptsLanguages([lang1, lang2, ...])

Parameters

ParameterDescription
langRequired. One or more language tags to test (for example 'en', 'fr', 'en-US'). Omit to list all accepted languages.
StringRequired. Best match among the languages you passed
falseRequired. You passed languages, but none are acceptable
String[]Required. Called with no arguments — all languages from Accept-Language

Return Value

With arguments, req.acceptsLanguages(...) returns a matching language string or false. With no arguments, it returns an array of languages from the Accept-Language header.

Express
const lang = req.acceptsLanguages('en', 'es') || 'en';
const all = req.acceptsLanguages(); // array from the header

Express.js Examples

Example 1 — Pick the Best Language

Offer English, Spanish, and French. Return the best match from Accept-Language.

Express
const express = require('express');
const app = express();

app.get('/welcome', (req, res) => {
  // Accept-Language: en;q=0.8, es  →  'es'
  const lang = req.acceptsLanguages('en', 'es', 'fr');
  if (!lang) {
    return res.status(406).send('No supported language');
  }
  res.json({ language: lang });
});

app.listen(3000);

How It Works

  1. Offer what you can serve. Pass only tags you have content for: 'en', 'es', 'fr'.
  2. Read client preference. Express uses the Accept-Language quality values (es defaults to q=1, ahead of en;q=0.8).
  3. Get one string or false. Here the best match is 'es'.
  4. Respond with that locale. The JSON body echoes the chosen language for the client.

Example 2 — List All Accepted Languages

Call with no arguments to see the full preference list from the header.

Express
const express = require('express');
const app = express();

app.get('/prefs', (req, res) => {
  // Accept-Language: en;q=0.8, es  →  ['es', 'en']
  const languages = req.acceptsLanguages();
  res.json({ languages });
});

app.listen(3000);

How It Works

  1. Skip the language list. No arguments means “parse the header only.”
  2. Get an array. Tags come back ordered by preference (higher q first).
  3. Useful for debugging. Log or return the list before you narrow to supported locales.
  4. Still not a translation API. You decide which tags your app can actually fulfill.

Example 3 — Default Locale Fallback

Prefer a match from your supported set; otherwise fall back to English instead of failing hard.

Express
const express = require('express');
const app = express();

const SUPPORTED = ['en', 'de', 'fr'];

app.get('/docs', (req, res) => {
  const lang = req.acceptsLanguages(SUPPORTED) || 'en';
  const copy = {
    en: 'Welcome to the docs',
    de: 'Willkommen in der Dokumentation',
    fr: 'Bienvenue dans la documentation'
  };
  res.json({ language: lang, message: copy[lang] });
});

app.listen(3000);

How It Works

  1. Pass an array. req.acceptsLanguages(SUPPORTED) is the same as listing each tag as an argument.
  2. Handle false. || 'en' picks a safe default when the client wants only unsupported languages.
  3. Map tag → content. A small dictionary keeps the demo clear; real apps use i18n files.
  4. Return both tag and message. The client sees which locale won and the localized string.

Runtime Support

req.acceptsLanguages() is an Express.js request method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only send the Accept-Language header.

✓ Server · Express 4 & 5

req.acceptsLanguages()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
req.acceptsLanguages() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Prefer the plural method name on Express 4 and 5.

Frequently Asked Questions

It negotiates the best match between languages your app supports and the client's Accept-Language header. With no arguments, it returns every language from that header as an array.
When you pass language arguments and none are acceptable, it returns false. Use a default locale or send 406 Not Acceptable.
Yes. Then it returns all languages from Accept-Language as an array, ordered by client preference (quality values).
No for new code. It was deprecated in Express 4.5.0 and removed in Express 5. Always use the plural req.acceptsLanguages().
No. It only picks a language tag. Translation files, formatting, and plural rules still need your i18n stack.

Next: req.app

Access the Express application instance from the request object.

req.app tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful