Express.js req.xhr Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

req.xhr is a Boolean that is true when the request’s X-Requested-With header is XMLHttpRequest (compared case-insensitively). Express documents this as a signal that a client library such as jQuery issued the request.

Use it as a hint to choose JSON vs HTML in older apps. It is not a security check. Prefer req.accepts() with an Accept header for modern content negotiation. Read the raw header with req.get('X-Requested-With').

Syntax

Read req.xhr in your Express.js route handler. Start with this line.

Express
req.xhr
req.get('X-Requested-With')?.toLowerCase() === 'xmlhttprequest'
CallWhat it does
req.xhrPrimary signature.
req.get('X-Requested-With')?.toLowerCase() === 'xmlhttprequest'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.xhr
req.get('X-Requested-With')?.toLowerCase() === 'xmlhttprequest'

Parameters

This Express.js property is read from the request or response — it has no call parameters.

Return Value

req.xhr is a Boolean that is true when X-Requested-With is XMLHttpRequest. It is a convenience property, not a security control.

Express
if (req.xhr) {
  return res.json({ ok: true });
}
res.render('ok');

Express.js Examples

Example 1 — Different Response for XHR vs Page Load

Classic pattern: JSON for XHR, HTML for a normal browser visit.

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

app.get('/profile', (req, res) => {
  if (req.xhr) {
    return res.json({ user: 'Ada', role: 'admin' });
  }
  res.type('html').send('

Ada

Profile page

'); }); app.listen(3000);

How It Works

  1. Read the flag. req.xhr is true only when the special header matches.
  2. XHR path. Return JSON with res.json for script clients.
  3. Browser path. Without the header, send a simple HTML page.
  4. Early return. return after JSON so you do not send both bodies.

Example 2 — Make fetch Set the Header

Show the server check and a matching browser fetch call.

Express
// --- Server ---
const express = require('express');
const app = express();

app.get('/api/ping', (req, res) => {
  res.json({ xhr: req.xhr });
});

app.listen(3000);

// --- Browser ---
fetch('/api/ping', {
  headers: { 'X-Requested-With': 'XMLHttpRequest' }
})
  .then((r) => r.json())
  .then(console.log); // { xhr: true }

How It Works

  1. Default fetch. No X-Requested-With → req.xhr is false.
  2. Add the header. Same value jQuery would send.
  3. Express compares. Case-insensitive match to xmlhttprequest.
  4. Echo for learning. Returning { xhr } makes the effect obvious while debugging.

Example 3 — Prefer req.accepts for Modern Clients

Same JSON-vs-HTML goal using standard content negotiation.

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

app.get('/profile', (req, res) => {
  // Prefer Accept over req.xhr for new code
  if (req.accepts('json')) {
    return res.json({ user: 'Ada' });
  }
  if (req.accepts('html')) {
    return res.type('html').send('

Ada

'); } res.status(406).send('Not Acceptable'); }); app.listen(3000); // fetch('/profile', { headers: { Accept: 'application/json' } })

How It Works

  1. Client declares preference. Accept: application/json is standard HTTP.
  2. req.accepts picks a match. Returns the best type or falsy if none fit.
  3. Respond accordingly. JSON, HTML, or 406 when nothing matches.
  4. Why switch. Works with bare fetch and avoids a non-standard header.

Runtime Support

req.xhr is an Express.js request property. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only send headers; they do not expose req.xhr.

✓ Server · Express 4 & 5

req.xhr

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.xhr 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 is a Boolean that is true when the X-Requested-With request header equals XMLHttpRequest (case-insensitive). Express documents this as a common signal from client libraries such as jQuery.
No. The Fetch API and many clients do not send X-Requested-With by default. You must add the header yourself if you rely on req.xhr.
No. Anyone can send or omit the header. Treat it as a convenience hint for response format, not authentication or authorization.
Prefer content negotiation with the Accept header and req.accepts('json') / req.accepts('html') for modern APIs.
No. It is a server-side Express property. Browsers only send headers; Express reads them.

Next: Response object

Move on from req properties to building HTTP responses with res.

res 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