Configuration Management
Configuration Management — purpose and prerequisites
Configuration is input from deployment rather than a value baked into source code. Node environment values arrive as strings or undefined; parse and validate them at startup, then pass a stable typed configuration into the application. Do not log credentials, and avoid relying on a type cast to turn strings into numbers.
What you’ll learn
- Explain the purpose of configuration management.
- Identify the typed contract, runtime responsibilities and common pitfalls.
- Run the example and extend its behavior with a practical exercise.
How the idea works
The function converts a string to a number, checks TCP port range, and accepts only recognized boolean strings. Production code can call config(process.env.PORT, process.env.DEBUG) once at startup.
TypeScript assertions such as process.env.PORT as unknown as number do not perform numeric conversion.
Visualize the flow
Run the checked TypeScript example
This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.
TypeScript source
type Settings = { port: number; debug: boolean };
function config(portText: string | undefined, debugText: string | undefined): Settings {
const port = Number(portText ?? "3000");
if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error("Invalid port");
if (debugText !== undefined && debugText !== "true" && debugText !== "false") throw new Error("Invalid debug");
return { port, debug: debugText === "true" };
}
console.log(config("8080", "true"));Verified output
{ port: 8080, debug: true }Compile: npx tsc --strict --target es2022 --module commonjs configuration-management.ts; run: node configuration-management.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.
Trace the execution
The function converts a string to a number, checks TCP port range, and accepts only recognized boolean strings. Production code can call config(process.env.PORT, process.env.DEBUG) once at startup.
Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.
Common mistake and correction
TypeScript assertions such as process.env.PORT as unknown as number do not perform numeric conversion.
Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.
Your turn: extend the example
Add a mode with allowed values test and production. Reject an unexpected value before starting the server.
Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.
Check your understanding
Key idea to remember
The function converts a string to a number, checks TCP port range, and accepts only recognized boolean strings. Production code can call config(process.env.PORT, process.env.DEBUG) once at startup.
Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.
Reaching the recap and completing the quizzes are separate milestones.