The booking page still can’t use the walks API. When it loads, the browser hides the answer to GET /walks because the response has no Access-Control-Allow-Origin header. When someone books a walk, the browser sends a preflight OPTIONS request first, gets no permission back, and never sends the POST. Loading the walks and booking a walk both fail because the server hasn’t said which page may call it.
In this lesson, you’ll make the API say so. You’ll add the booking page’s origin to the config as a new setting, let the cors package send the right headers, and check each header with curl -i before you book a walk from the page. Start with the setting, because the allowed origin changes from one machine to the next.
Add the Booking Page’s Origin to the Config
The server needs to know which origin to allow, and that answer depends on where it runs. On your machine, the page is at http://localhost:5173. When the co-op puts the page and the API online, the page will have a different address. That makes the origin a setting, so it goes in the environment and through the Zod schema in config.ts, like PORT and DATA_FILE.
Add ALLOWED_ORIGIN to the schema, and pass it on in config:
config.ts
const envSchema = z.object({
PORT: z.coerce.number().int().min(1).max(65535)
.default(3000),
DATA_FILE: z.string().min(1),
PRETTY_JSON: z.stringbool().default(false),
ALLOWED_ORIGIN: z
.url({ protocol: /^https?$/, abort: true })
.refine((value) => value === new URL(value).origin,
"expected an origin like http://localhost:5173"),
});
// ...
export const config = {
port: result.data.PORT,
dataFile: result.data.DATA_FILE,
prettyJson: result.data.PRETTY_JSON,
allowedOrigin: result.data.ALLOWED_ORIGIN,
};
The new validator accepts only an HTTP or HTTPS origin written exactly as the browser sends it in the Origin header. That’s the scheme, host, and port, with no path, no trailing slash, no default port, and nothing in capitals. The cors package compares the two as text, so a value that differs by a single character would never match.
z.url() accepts only text that parses as a URL. Its protocol option is a regular expression that the URL’s scheme must match, and /^https?$/ allows http and https only. Without it, localhost:5173 would pass as a URL whose scheme is localhost. With abort: true, Zod skips the next check when the value isn’t a URL at all.
.refine() adds a check of your own. You give it a function that returns true for a good value and the message to show for a bad one. A URL’s .origin property holds only its scheme, host, and port, normalized the way browsers send them, so a setting that differs from its own origin can never match the Origin header.
Like DATA_FILE, the new setting has no default, so if you start the API now, it stops with Invalid setting ALLOWED_ORIGIN instead of failing later in a neighbor’s browser. Add the setting to .env and to .env.example so the next person knows the API needs it:
.env.example
PORT=3000
DATA_FILE=data/walks.json
PRETTY_JSON=false
ALLOWED_ORIGIN=http://localhost:5173
To see the check work, add a slash at the end of the value in .env, as you might when you copy the page’s address from the browser, and start the API: