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.
- 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.
- Server Setup: The Express server runs on port 3000 and serves both the API and the frontend HTML.
- API Caching:
- Every
/api/users
response includes aCache-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 returns304 Not Modified
.
- Every
- Frontend:
- Open
http://localhost:3000
in your browser. - Click "Fetch Users" to make an API request and see caching in action.
- Open
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.
- Install dependencies:
npm install
- Start the server:
node server.js
- Open your browser at http://localhost:3000