Skip to content

Configuration

Environment-based configuration for all application settings.

What You'll Learn

  • All available environment variables
  • How configuration is loaded and validated
  • Environment-specific settings
  • Security best practices

Environment Variables

VariableDescriptionDefaultRequired
PORTServer port3000No
NODE_ENVEnvironment modedevelopmentNo
DATABASE_URLPostgreSQL connection string-Yes
REDIS_URLRedis connection stringredis://localhost:6379No
JWT_SECRETSecret for signing JWTs-Yes
JWT_EXPIRES_INLegacy token expiry7dNo
ACCESS_TOKEN_EXPIRES_INAccess token TTL15mNo
REFRESH_TOKEN_EXPIRES_INRefresh token TTL7dNo
CORS_ORIGINAllowed CORS originshttp://localhost:3000No
RATE_LIMIT_WINDOW_MSRate limit window900000 (15 min)No
RATE_LIMIT_MAX_REQUESTSMax requests per window100No

Complete .env Example

env
# Server
PORT=3000
NODE_ENV=development

# Database (Required)
DATABASE_URL="postgresql://postgres:password@localhost:5432/express_prisma_db"

# Redis
REDIS_URL="redis://localhost:6379"

# JWT Authentication (Required - change in production!)
JWT_SECRET="your-super-secret-jwt-key-change-in-production"
JWT_EXPIRES_IN="7d"
ACCESS_TOKEN_EXPIRES_IN="15m"
REFRESH_TOKEN_EXPIRES_IN="7d"

# CORS
CORS_ORIGIN="http://localhost:3000"

# Rate Limiting
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=100

Setup

  1. Copy the example file:
bash
cp .env.example .env
  1. Edit with your values:
bash
# Generate a secure JWT secret
openssl rand -hex 64

Config Module

Configuration is centralized in src/config/index.ts:

typescript
import config from './config';

// Access configuration
console.log(config.port);           // 3000
console.log(config.nodeEnv);        // 'development'
console.log(config.databaseUrl);    // PostgreSQL URL
console.log(config.redisUrl);       // Redis URL
console.log(config.jwtSecret);      // JWT signing secret

Type-Safe Access

The config object is fully typed:

typescript
interface Config {
  port: number;
  nodeEnv: 'development' | 'production' | 'test';
  databaseUrl: string;
  redisUrl: string;
  jwtSecret: string;
  accessTokenExpiresIn: string;
  refreshTokenExpiresIn: string;
  corsOrigin: string;
  rateLimitWindowMs: number;
  rateLimitMaxRequests: number;
}

Validation

Required variables are validated at startup. The app throws an error if DATABASE_URL or JWT_SECRET are missing:

Error: Missing required environment variable: JWT_SECRET

Environment-Specific Settings

Development

env
NODE_ENV=development
CORS_ORIGIN="http://localhost:3000"

Production

env
NODE_ENV=production
JWT_SECRET="your-very-long-random-production-secret"
CORS_ORIGIN="https://your-app.com"
DATABASE_URL="postgresql://user:pass@prod-db:5432/prod_db"
REDIS_URL="redis://prod-redis:6379"

Production Security

  • Use strong, unique secrets (64+ characters)
  • Never commit .env to version control
  • Use environment variable management (Docker secrets, Kubernetes secrets, etc.)
  • Enable HTTPS and update CORS accordingly

Docker Configuration

When using Docker Compose, environment variables can be set in docker-compose.yml:

yaml
services:
  app:
    environment:
      - NODE_ENV=production
      - DATABASE_URL=postgresql://postgres:password@db:5432/mydb
      - REDIS_URL=redis://redis:6379
      - JWT_SECRET=${JWT_SECRET}  # From host .env

Or use an env file:

yaml
services:
  app:
    env_file:
      - .env.production

Released under the MIT License.