Express.js req.fresh Property

Last Updated : 26 Aug, 2026

The req.fresh property returns true when the client's cache is considered fresh according to the request and response cache headers. Otherwise, it returns false.

frame_3273

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 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:

NodeProj


Example 1: Checking Cache Freshness

javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
    res.set('Cache-Control', 'public, max-age=60');
    res.set('ETag', '"gfg-123"');
    console.log('Fresh:', req.fresh);
    res.send('GeeksforGeeks');
});
app.listen(PORT, () => {
    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:

Console Output:

Screenshot-2026-08-12-150152

Browser Output:

Now open your browser and go to http://localhost:3000/, now you can see the following output on your browser:

Screenshot-2026-08-12-150136

Example 2: Checking Freshness with ETag

JavaScript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
    res.set('ETag', '"gfg-123"');
    if (req.fresh) {
        res.status(304).end();
    } else {
        res.send('GeeksforGeeks');
    }
});
app.listen(PORT, () => {
    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:

Console Output:

Server listening on PORT 3000

Browser Output: First make GET request to http://localhost:3000/ now you can see the following output on your browser:

Screenshot-2026-08-12-150136

The response contains the following ETag:

ETag: "gfg-123"

Now make another GET request to the same URL and add the request header:

Key: If-None-Match,  Value: "gfg-123"

Console Output:

Fresh: true

Browser Output:

Screenshot-2026-08-12-154134

Working of req.fresh

  • The client sends an HTTP request to the Express server.
  • Express checks the request against the response's cache-related headers.
  • Express determines whether the cached response is still fresh.
  • The req.fresh property returns true if the response is considered fresh.
  • Otherwise, it returns false.

Use Cases of req.fresh

  • Checking whether a cached response is still valid.
  • Reducing unnecessary response transfers.
  • Supporting HTTP conditional requests.
  • Improving performance for cacheable resources.
  • Working with ETag and Last-Modified based caching.
Comment