When Priya’s booking page books a walk, the browser doesn’t send the POST straight away. It first sends an OPTIONS request to the same path, asking your API whether the page may send it. That’s why the API’s terminal showed an OPTIONS line and no POST.
Key points:
- A request that a plain HTML form could send without any script, such as a
GET, or aPOSTof form data or plain text, goes straight out. Priya’s page has a form, but its script sends the booking withfetch()andContent-Type: application/json, which no plain form can do. That content type, a method such asDELETE, or a custom header the page adds gets a preflight request first. - The preflight is an
OPTIONSrequest with three headers that matter:Originsays which page is asking,Access-Control-Request-Methodnames the method it wants to send, andAccess-Control-Request-Headerslists the headers it set that need permission. - Your API has no
OPTIONSroute, but/walkshasGETandPOSTroutes, so Express answered for you with200 OKand anAllowheader. That answer has noAccess-Control-Allow-Originheader, so the browser dropped thePOSTand the page’sfetch()call failed with aTypeError. YourPOSThandler never ran, because the browser never sent thePOST. - For the booking’s
POSTto go out, the preflight needs a successful answer withAccess-Control-Allow-Originnaming the page’s origin andAccess-Control-Allow-HeadersnamingContent-Type. With only the first, the browser still refuses. - Each request header in the preflight has a matching answer header:
Access-Control-Request-Headersis answered byAccess-Control-Allow-Headers, andAccess-Control-Request-MethodbyAccess-Control-Allow-Methods.POST, likeGET, never needs listing inAccess-Control-Allow-Methods, so this booking doesn’t need it. - The response to the
POSTitself needsAccess-Control-Allow-Origintoo. Without it, the walk is still booked, but the page can’t read the answer.
Here’s the booking’s preflight, with the headers that matter:
OPTIONS /walks HTTP/1.1
Origin: http://localhost:5173
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type