Until now the server had its settings written into the code: the port 3000, the database file shop.db, and the storefront's origin http://localhost:5173. Those are right on your computer, but a real server needs other values. The same code should run in both places; only the settings change.
A program gets such settings from its environment variables, set by whoever starts it. Node puts them in process.env, always as text, and a variable nobody set is undefined. With || you fall back to a development value:
const greeting = process.env.GREETING || "Hello";
const workers = Number(process.env.WORKERS) || 4;
With nothing set, greeting is "Hello" and workers is 4. With WORKERS=8, Number turns the text "8" into the number 8.
The given server/app.js and server/index.js read config.port, config.databasePath and config.clientOrigin from server/config.js. The check starts the server with other values (staging.db is a copy of shop.db with one more product, a Kettle) and must see:
PORT=8080 DATABASE_PATH=staging.db CLIENT_ORIGIN=https://shop.example.com
open the page → Showing 3 of 9
search "kettle" → item: Kettle $35
GET /api/products/9 → 200 Access-Control-Allow-Origin: https://shop.example.com
{"id": 9, "name": "Kettle", "price": 35, "stock": 5}
node server/index.js → The shop's API listens on http://localhost:8080
with no variables set → port: 3000, databasePath: "shop.db", clientOrigin: "http://localhost:5173"
Your task: in server/config.js, fill the exported object with port, databasePath and clientOrigin, each read from its environment variable, with the development value when the variable is not set.
Assignment
Follow the Coding Tutorial to fill server/config.js with the server's three settings.
HintPORT arrives as text, so turn it into a number with Number(…) before the ||. Each property of the object ends with a comma.
Free plan: 30 tutor messages. PRO is unlimited.
