Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Configuration, CORS and Logging for Node APIs (Summary)

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:

Language: Shell
$ 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:

Language: Shell
$ 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:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 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, and log every request and error usefully, without leaking secrets or personal data.

00:23 First, each machine’s .env sets its values. A config module checks them at startup, so a missing required setting or bad value stops the server and names the setting.

00:36 Next, a CORS error hid the walks list from Priya’s page, though the server answered. For a JSON booking, the browser asks first with an OPTIONS preflight. The cors middleware answers, and only the booking page’s origin gets the allow header.

00:54 Last, each finished response leaves one JSON line, if the log level allows. Its request id matches the response’s header, so a user’s report finds it. Ama’s phone number never reaches the log.

01:09 You practiced each job in an exercise, then combined them in the project.

01:15 Settings come from the environment, and .env stays out of Git. Check every setting once, at startup. CORS protects the user’s browser, not your server, so the API still checks every request.

01:30 Log one JSON line per finished request, with its request id. A server error’s stack goes to the log, never the response.

01:41 Next on this path is Testing, Documenting and Building a Node API. There, you’ll test an API through real HTTP requests, document every endpoint, and build a REST API from a brief.

01:55 Thanks for getting the co-op’s walks API ready.

Become a Member to join the conversation.