Express.js req.is() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

In Express.js, req.is() checks whether the request’s Content-Type matches a MIME type or shorthand you pass. On a match it returns that type string; otherwise false. If the request has no body, it returns null.

Use it before reading req.body to enforce JSON or form payloads. It is powered by the type-is library. For what the client wants in the response, use req.accepts() instead.

Syntax

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

Express
req.is(type)
req.is(type1, type2, ...)
req.is([type1, type2, ...])
CallWhat it does
req.is(type)Primary signature.
req.is(type1, type2, ...)Related call form.
req.is([type1, type2, ...])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.is(type)
req.is(type1, type2, ...)
req.is([type1, type2, ...])

Parameters

ParameterDescription
typeRequired. MIME type (application/json), extension shorthand (json, html), wildcard (text/*), comma list, or array.
StringRequired. Header matches — value is the type you asked for that matched (for example 'json' or 'text/*')
falseRequired. Body present, but no listed type matches
nullRequired. Request has no body (common on GET)

Return Value

req.is(type) returns the first matching type string you passed (for example 'json'), or a falsy value (false/null) when there is no match or no body type to test.

Express
if (!req.is('json')) {
  return res.status(415).send('JSON only');
}
// match may be the string 'json', not boolean true

Express.js Examples

Example 1 — Require a JSON Body

Reject non-JSON posts with 415 before handling the payload.

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

app.use(express.json());

app.post('/api/orders', (req, res) => {
  if (!req.is('application/json')) {
    return res.status(415).send('JSON payload required');
  }
  res.json({ ok: true, body: req.body });
});

app.listen(3000);

How It Works

  1. Parse JSON when present. express.json() fills req.body for matching types.
  2. Gate with req.is. req.is('application/json') returns a string on match, or a falsy value otherwise.
  3. Reject early. Falsy → 415 and no further handling.
  4. Accept on match. Echo the parsed body as JSON.

Example 2 — Allow JSON or URL-Encoded Form

Accept either shorthand and branch on the returned type.

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

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.post('/submit', (req, res) => {
  const type = req.is('json', 'urlencoded');
  if (!type) {
    return res.status(415).send('Unsupported content type');
  }
  res.send('Parsed as: ' + type);
});

app.listen(3000);

How It Works

  1. Mount both parsers. JSON and form bodies can both land in req.body.
  2. Pass several types. req.is('json', 'urlencoded') returns the first match.
  3. Falsy means reject. Anything else gets 415.
  4. Use the string. The return value tells you which shorthand matched.

Example 3 — Match with a Wildcard

Accept any text/* type, the same way the official docs demonstrate.

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

app.post('/notes', (req, res) => {
  // Content-Type: text/html; charset=utf-8
  const kind = req.is('text/*');
  if (!kind) {
    return res.status(415).send('text/* required');
  }
  res.send('Matched: ' + kind);
});

app.listen(3000);

How It Works

  1. Wildcards widen the match. text/* covers text/html, text/plain, and similar.
  2. Parameters are ignored for the check. charset=utf-8 does not block a match.
  3. Return echoes the pattern. On success you get 'text/*' (the type you tested).
  4. Non-text types fail. JSON returns false → 415.

Runtime Support

req.is() is an Express.js request method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers do not execute it — they only send HTTP requests to your app.

✓ Server · Express 4 & 5

req.is()

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.is() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It checks the request Content-Type header against one or more MIME types or shorthand names (like json or html) and returns the match, false, or null.
A matching type string (for example 'json' or 'text/html'), false when nothing matches, or null when the request has no body.
req.is() inspects the incoming Content-Type (what the client sent). req.accepts() inspects the Accept header (what the client wants back).
Yes. Pass multiple arguments or an array, such as req.is('json', 'urlencoded') or req.is(['json', 'html']). The first match wins.
No. It is a server-side Express request method. Browsers only send HTTP requests; your Node.js app calls req.is().

Next: req.method

Read the HTTP verb (GET, POST, PUT, …) for the current request.

req.method 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