Skip to article content
FrontendXLesson 07 / 24

Configuration, secrets and twelve-factor discipline

Build a Node HTTP API from scratch, model data in SQL and MongoDB, and reason about transactions, indexes, caching and authentication so the endpoint you ship stays correct under load.

intermediate · about 45 minutes · 2 exercises

  • Explain
  • Debug

What you will be able to do

#
  • Keep configuration in the environment and out of source control.
  • Validate configuration at startup and fail fast.
  • Separate build, release and run stages.

Overview

#

Twelve-factor apps separate configuration from code. The same artifact runs in every environment; only the injected environment variables differ. Secrets never live in the repository.

Validate at startup

snippet.js
function required(name) {
  const value = process.env[name];
  if (!value) throw new Error(`Missing required env var: ${name}`);
  return value;
}

const config = {
  port: Number(process.env.PORT || 3000),
  databaseUrl: required('DATABASE_URL'),
  jwtSecret: required('JWT_SECRET'),
};

Failing at startup is better than failing on the first request, because the failure is loud, early and not tied to a user action.

Never log secrets

Redact tokens, passwords and connection strings before logging. A leaked secret in a log aggregator is a production incident.

Common pitfalls

  • Committing a .env file with real credentials.
  • Defaulting a secret to a known value like 'changeme' in production.
  • Reading config inside hot request paths so a missing value fails intermittently.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Debug

    This function leaks the database password into logs. Rewrite it to redact the secret.

    starter.javascript
    function logConnection(url) {
      console.log('Connecting to', url);
    }

    Reveal the reference answer
    reference.javascript
    function logConnection(url) {
      const redacted = url.replace(/\/\/[^@]+@/, '//***@');
      console.log('Connecting to', redacted);
    }

    Authored expected output: Logs 'Connecting to postgres://***@host/db' with credentials removed.

    A connection URL contains user:password before the @. Replace that segment before logging so the secret never reaches the log sink.

  2. Exercise 2Explain

    Explain why the same build artifact should run in staging and production.

    starter.text
    // Describe build, release and run stages.
    

    Reveal the reference answer
    reference.text
    If each environment builds its own artifact, the code that reaches production is not the code that was tested. Building once and injecting configuration at run time means staging and production run byte-identical code, and only environment variables differ.

    Authored expected output: An explanation of one immutable build promoted through environments with configuration injected at run time.

    This is the build, release, run separation from twelve-factor and it makes promotions trustworthy.

Common misconceptions

#
  • A .env file is safe to commit: it is a local convenience, not a secret store.
  • Config can be read lazily: validating once at startup prevents partial, hard-to-debug failures.

Follow-up practice

#
  • backend-databases-relational-modeling

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.