If-Match: two curls, one 412
Two tabs save the same note. Last write wins. The first tab's words vanish. Two terminals on your laptop are two tabs. You GET. You type. You PUT. Save this as if-match-server.mjs . Run node if-match-server.mjs . import http from " node:http " ; let note = { text : " hello " }; let etag = " 1 " ; http . createServer (( req , res ) => { const reply = ( status , body ) => { const json = JSON .…
Two tabs overwrite the same note. The first tab's content vanishes. Two endpoints on your machine are two tabs. You GET. You type. You PUT. Save this as if-match-server.mjs . Execute the file with node if-match-server.mjs . Import the http module. Create a note object with text as "hello". Establish an ETag of 1. Construct a server using http.createServer.
Respond with a 404 error if the URL does not match /note . Respond with a 200 response and the note's text and ETag for GET requests. Reject non-PUT requests with a 405 error. Capture raw data from requests and store it in raw. If the IF-Match header matches the ETag, proceed with the PUT request. Parse the JSON request body and update the note's text and increment the ETag.
Return a 200 response with the updated note and ETag. Listen on port 3456 for incoming connections at localhost.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.