Configuration and logging
Understand the idea
Port numbers and log lines are part of the structure. They should not be invented in the middle of a route.
The idea
The should read settings in one function. setting returns the value when it is non-empty and the fallback otherwise. This runner does not set APP_PORT, so the first run prints the fallback. The second run passes a source object so you can see an override without depending on the machine. The log is JSON.stringify of level, msg, and port. That record is the same idea as the structured logs in the later lesson. A sentence in console.log is not a substitute.
What changes
| Habit | What drifts | What to do instead |
|---|---|---|
| env read in each route | The fallback | One setting function |
| A sentence in the log | You cannot filter it | A JSON record with level and msg |
| A default buried in a handler | The next file invents another | Pass the value in from the edge |
The command
Fallback port and a JSON log
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Override from a source object
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
The first run prints 3000, then a JSON line whose port is 3000 and whose level is info. The second run prints 4173 and level debug.
Guided challenge
Use 3000 as the fallback port
setting falls back to 80 when APP_PORT is missing. Change the fallback to 3000 so the printed port matches the lesson default.
Use 3000 as the fallback port
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
3000
Hints 0/2
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.