Express.js req.fresh Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

In Express.js, req.fresh is true when the response is still fresh in the client’s cache — the cached copy still matches your validators. Then you can send 304 Not Modified instead of the full body.

Express compares conditional request headers (If-None-Match, If-Modified-Since) with response validators you set (ETag, Last-Modified). If the client sends Cache-Control: no-cache, req.fresh is false so a full response is sent. This is a server-side property on the request object.

Syntax

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

Express
req.fresh   // Boolean
req.stale   // Boolean — opposite of req.fresh
res.set('ETag', '"v1"')
if (req.fresh) return res.status(304).end()
CallWhat it does
req.fresh // BooleanPrimary signature.
req.stale // Boolean — opposite of req.freshRelated call form.
res.set('ETag', '"v1"')Related call form.
if (req.fresh) return res.status(304).end()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.fresh   // Boolean
req.stale   // Boolean — opposite of req.fresh
res.set('ETag', '"v1"')
if (req.fresh) return res.status(304).end()

Parameters

ParameterDescription
Response validatorRequired. Without ETag / Last-Modified, freshness has nothing to compare
Conditional request headersOptional. Browsers send them on revalidation; APIs can set them too
GET / HEAD (typical)Optional. Freshness checks are meant for safe cache revalidation

Return Value

req.fresh is a Boolean property. It is true when the request is still fresh versus the current response cache validators (ETag / Last-Modified).

Express
res.set('ETag', etag);
if (req.fresh) {
  return res.status(304).end();
}
res.send(body);

Express.js Examples

Example 1 — Send 304 When the ETag Matches

Set an ETag, then use req.fresh to skip the JSON body when the client already has that version.

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

app.get('/news', (req, res) => {
  res.set('ETag', '"news-v1"');

  if (req.fresh) {
    return res.status(304).end();
  }

  res.json({ items: ['a', 'b', 'c'] });
});

app.listen(3000);

How It Works

  1. Advertise a version. ETag: "news-v1" identifies this representation.
  2. Client revalidates. Next time it sends If-None-Match: "news-v1".
  3. Express compares. After the header is set, req.fresh is true on a match.
  4. Save bandwidth. 304 tells the client to reuse its cache; no JSON is sent.

Example 2 — Freshness with Last-Modified

Use a last-modified date instead of (or alongside) an ETag.

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

const modified = new Date('2026-05-01T00:00:00Z');

app.get('/article', (req, res) => {
  res.set('Last-Modified', modified.toUTCString());

  if (req.fresh) {
    return res.status(304).end();
  }

  res.send('Latest article content');
});

app.listen(3000);

How It Works

  1. Publish a timestamp. Last-Modified is the resource’s known change time.
  2. Client sends it back. If-Modified-Since asks “has it changed since then?”
  3. req.fresh answers. Unchanged → true → 304.
  4. Changed content. Update modified, and clients with the old date get a full 200.

Example 3 — Let res.send Apply 304

After setting an ETag, res.send can flip to 304 automatically when req.fresh is true.

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

app.get('/about', (req, res) => {
  res.set('ETag', '"about-1"');
  // If req.fresh, Express may respond 304 and omit the body
  res.send('About CodeToFun');
});

app.listen(3000);

How It Works

  1. Set the validator first. Same rule: ETag must exist before freshness is evaluated.
  2. Call res.send. Internally Express checks req.fresh.
  3. Match → 304. Status becomes Not Modified and the body is dropped.
  4. Miss → normal 200. The string body is sent as usual.

Runtime Support

req.fresh is an Express.js request property for HTTP cache validation. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers send conditional headers; they do not expose this property.

✓ Server · Express 4 & 5

req.fresh

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.fresh Express on Node.js

Bottom line: Set an ETag or Last-Modified, then use req.fresh (or res.send) to return efficient 304 responses.

Frequently Asked Questions

It is true when the client's cached copy is still valid compared with your response validators (ETag / Last-Modified). Fresh cache → you can send 304 Not Modified.
req.stale is the opposite: true when the cache is not valid and you should send the full body.
Clients send If-None-Match or If-Modified-Since. You set ETag or Last-Modified on the response. Express compares them to compute req.fresh.
Yes. After you set validators, res.send can turn a matching conditional GET into 304 and omit the body.
If the client sends Cache-Control: no-cache (a forced reload), req.fresh is false so you send a full response.

Next: req.get()

Read any HTTP request header by name (case-insensitive).

req.get() 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