MyApp Docs ← Back to Getting Started

Advanced Setup Guide

Table of Contents

Environment Variables

Define all required variables in a .env file at the project root. Example:

# .env
APP_PORT=8080
DB_HOST=127.0.0.1
DB_USER=admin
DB_PASS=secret
LOG_LEVEL=debug

Load them in your application:

// config.js
import dotenv from 'dotenv';
dotenv.config();

export const config = {
    port: process.env.APP_PORT || 3000,
    db: {
        host: process.env.DB_HOST,
        user: process.env.DB_USER,
        password: process.env.DB_PASS,
    },
    logLevel: process.env.LOG_LEVEL || 'info',
};

Custom Logging

Integrate winston for structured logs.

// logger.js
import winston from 'winston';

const logger = winston.createLogger({
    level: process.env.LOG_LEVEL || 'info',
    format: winston.format.combine(
        winston.format.timestamp(),
        winston.format.json()
    ),
    transports: [new winston.transports.Console()],
});

export default logger;

Use it across your code:

// app.js
import logger from './logger.js';
logger.info('Application started', { port: config.port });

Docker Compose Integration

Run the full stack with Docker Compose:

# docker-compose.yml
version: '3.8'
services:
  app:
    build: .
    ports:
      - "${APP_PORT:-8080}:8080"
    env_file:
      - .env
    depends_on:
      - db
  db:
    image: postgres:15
    environment:
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASS}
      POSTGRES_DB: myapp
    volumes:
      - db-data:/var/lib/postgresql/data
volumes:
  db-data:

CI/CD Pipeline (GitHub Actions)

Sample workflow to build, test, and deploy Docker images:

# .github/workflows/ci-cd.yml
name: CI/CD

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Node
        uses: actions/setup-node@v3
        with:
          node-version: '20'
      - run: npm ci
      - run: npm test
      - name: Build Docker image
        run: |
          docker build -t myapp:${{ github.sha }} .
          echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
          docker push myapp:${{ github.sha }}

FAQ

How do I override a default environment variable? +

Create a .env.local file and list the variables you want to override. Ensure this file is added to .gitignore.

Can I use a different logging service? +

Yes. Replace the Winston transport with any compatible transport (e.g., winston-loggly-bulk for Loggly or @google-cloud/logging-winston for GCP).