Full Stack JS

Chapter 19
0%

Chapter 19: Environment & Build

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:

.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!"

ConceptWhat It IsAnalogy
ImageA blueprint/recipe for your containerA recipe card
ContainerA running instance of an imageThe dish you cooked from the recipe
DockerfileInstructions to build an imageStep-by-step cooking instructions
Docker ComposeRun multiple containers togetherA 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

CategoryDoDon't
SecretsUse environment variablesHard-code API keys in source code
PasswordsHash with bcryptStore in plain text
InputValidate and sanitize everythingTrust user input blindly
DependenciesRun npm audit regularlyIgnore security warnings
HTTPSAlways use HTTPS in productionServe over plain HTTP
HeadersUse helmet for security headersUse 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?

To make the API respond faster
To reduce database size
To prevent brute force password guessing attacks
To limit the number of registered users
← Chapter 18: Git & GitHub Chapter 20: Deploy to Production →