Learn how to configure environments, build production-ready apps, and containerize with Docker.
Env Variables
Production Build
Docker Basics
Security
Quiz
Environment Variables
Environment variables are like spy instructions. They are secret, separate from the mission briefing (your code), and change depending on the mission (development vs production). You would never print classified info in a public newspaper — and you should never hard-code secrets like API keys or passwords in your source code.
Environment variables let you store configuration that changes between environments (development, testing, production) without modifying your code:
Database URLs — Different databases for dev and production
API keys — Secret keys for third-party services
JWT secrets — Keys for signing authentication tokens
Port numbers — Different ports for different environments
.env — Development Environment
JavaScript — Using Environment Variables
NEVER commit .env files to Git! Always add .env to your .gitignore. If you accidentally commit secrets, assume they are compromised and rotate them immediately.
Production Build
Building for production is like packing a suitcase for a trip. During development, your clothes (code) are scattered around your room — easy to find and modify. For the trip (production), you fold everything neatly, remove what you don't need, and compress it all into a compact suitcase. The result is smaller, faster, and ready to go.
Frontend Build (React/Vite)
Terminal — Building a React App
Backend Preparation
package.json — Production Scripts
devDependencies are only installed during development. In production, run npm install --production (or npm ci) to skip them and keep the deployment lean.
Docker Basics
Docker is like a shipping container. Before shipping containers, loading cargo onto ships was chaos — different shapes, sizes, and requirements. Shipping containers standardized everything: pack your goods in a container, and it works on any ship, truck, or train. Docker does the same for software: pack your app in a container, and it runs the same on any computer, any server, any cloud.
Docker solves the classic problem: "But it works on my machine!"
Concept
What It Is
Analogy
Image
A blueprint/recipe for your container
A recipe card
Container
A running instance of an image
The dish you cooked from the recipe
Dockerfile
Instructions to build an image
Step-by-step cooking instructions
Docker Compose
Run multiple containers together
A multi-course meal plan
Dockerfile — Backend API
Terminal — Docker Commands
docker-compose.yml — Multi-Container Setup
With Docker Compose, docker compose up starts your entire stack (API + database) with one command. docker compose down stops everything.
Security Best Practices
Before deploying, make sure your app follows these security practices:
Terminal — Install Security Packages
JavaScript — Security Middleware
Security Checklist
Category
Do
Don't
Secrets
Use environment variables
Hard-code API keys in source code
Passwords
Hash with bcrypt
Store in plain text
Input
Validate and sanitize everything
Trust user input blindly
Dependencies
Run npm audit regularly
Ignore security warnings
HTTPS
Always use HTTPS in production
Serve over plain HTTP
Headers
Use helmet for security headers
Use default Express headers
📝 Chapter 19 Quiz
1. Where should you store sensitive configuration like API keys?
Directly in your JavaScript source code
In a comment at the top of each file
In a .env file (excluded from Git)
In the package.json file
2. What does npm run build do for a React app?
Starts the development server
Creates optimized, minified static files for production
Installs all dependencies
Runs unit tests
3. What is a Dockerfile?
A file with instructions to build a Docker image
A running container
A database configuration file
A Git configuration file
4. What does the helmet package do?
Encrypts all database queries
Hashes passwords automatically
Blocks all incoming requests
Sets HTTP security headers to protect against common attacks
5. Why is rate limiting important for an authentication endpoint?