You’ve finished Configuration, CORS and Logging for Node APIs. The Elm Lane walks API is ready for Priya’s booking page. You can now:
- Configure an Express API through environment variables
- Let a browser front end on another origin call it
- Log every request and error usefully, without leaking secrets or personal data
Each machine sets its values in a .env file, and a config module checks them at startup, so a missing required setting or bad value stops the server and names the setting:
$ npm start
Invalid setting ALLOWED_ORIGIN: Invalid input: expected string, received undefined
A CORS error hid the walks list from Priya’s page, though the server had answered. For a JSON booking, the browser asks first with an OPTIONS preflight, and the cors middleware answers it. Only the booking page’s origin gets the Access-Control-Allow-Origin header:
$ curl -i -X OPTIONS -H "Origin: http://localhost:5173" \
-H "Access-Control-Request-Method: POST" \
-H "Access-Control-Request-Headers: content-type" \
http://localhost:3000/walks
HTTP/1.1 204 No Content
X-Powered-By: Express
X-Request-Id: a3ab6838-552d-4c71-826b-1612f165b3b8
Access-Control-Allow-Origin: http://localhost:5173
Vary: Origin
Access-Control-Allow-Methods: GET,POST
Access-Control-Allow-Headers: Content-Type
...
Each finished response leaves one JSON line, if the log level allows. Its request_id matches the response’s X-Request-Id header, so a user’s report finds it. Ama Mensah’s phone number in Mabel’s booking never reaches the log. Here’s the booking’s line, wrapped for display: