Skip to content

This repository demonstrates basic HTTP caching techniques in a Node.js Express server, including the use of Cache-Control headers and ETag for efficient API responses.

Notifications You must be signed in to change notification settings

Shravanch19/cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Caching Demo

This repository demonstrates basic HTTP caching techniques in a Node.js Express server, including the use of Cache-Control headers and ETag for efficient API responses.

Features

  • Express.js API: Serves a simple /api/users endpoint with dummy user data.
  • Cache-Control Header: Instructs browsers and proxies to cache API responses for 20 seconds.
  • ETag Support: Uses an MD5 hash of the response data to provide ETag headers, allowing clients to avoid downloading unchanged data.
  • 304 Not Modified: If the client sends a matching ETag, the server responds with a 304 status, saving bandwidth.
  • Frontend Demo: index.html provides a button to fetch users and display the response status, headers, and data.

How It Works

  1. Server Setup: The Express server runs on port 3000 and serves both the API and the frontend HTML.
  2. API Caching:
    • Every /api/users response includes a Cache-Control: public, max-age=20 header (cache for 20 seconds).
    • The server generates an ETag for the user data. If the client sends an If-None-Match header with a matching ETag, the server returns 304 Not Modified.
  3. Frontend:
    • Open http://localhost:3000 in your browser.
    • Click "Fetch Users" to make an API request and see caching in action.

Files

  • server.js: Express server with API and caching logic.
  • index.html: Simple frontend to test the API and caching.
  • package.json: Project metadata and dependencies.

Getting Started

  1. Install dependencies:
    npm install
  2. Start the server:
    node server.js
  3. Open your browser at http://localhost:3000

About

This repository demonstrates basic HTTP caching techniques in a Node.js Express server, including the use of Cache-Control headers and ETag for efficient API responses.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published