Skip to content

Commit 39c5aed

Browse files
committed
update API response status code from 304 to 204 for cache hits
1 parent 48385de commit 39c5aed

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Calling the API
6363
6464
```bash
6565
curl -X POST http://localhost:3000/create -H "x-request-id: 123" # 200 -> Resource created!
66-
curl -X POST http://localhost:3000/create -H "x-request-id: 123" # 304
66+
curl -X POST http://localhost:3000/create -H "x-request-id: 123" # 204
6767
# after 5 seconds
6868
curl -X POST http://localhost:3000/create -H "x-request-id: 123" # 200 -> Resource created!
6969
```

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ export function idempotencyMiddleware({
6666
.get(idempotencyKey)
6767
.then(function (cachedResponse) {
6868
if (cachedResponse) {
69-
// Cached response found: return 304 Not Modified to prevent reprocessing
70-
res.statusCode = 304
69+
res.statusCode = 204
70+
res.setHeader('X-Idempotency-Status', 'hit')
7171
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
7272
res.end()
7373
} else {

test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ test('idempotencyMiddleware - idempotency key present and cache hit', async (t)
110110
)
111111
assert.strictEqual(
112112
res.statusCode,
113-
304,
114-
'Should return 304 Not Modified if cache hit',
113+
204,
114+
'Should return 204 Not Modified if cache hit',
115115
)
116116
assert.strictEqual(
117117
res.headers['content-type'],

0 commit comments

Comments
 (0)