# MEAN Stack

> MongoDB, Express, Angular, Node.js full-stack setup.

- Category: Full Web Stacks
- Difficulty: intermediate
- Components: mongodb, node, nginx
- Tags: mongodb, express, angular, nodejs
- Source: https://docker.recipes/full-stacks/mean-stack

## Overview

The MEAN stack combines MongoDB, Express.js, Angular, and Node.js into a JavaScript-first full-stack development platform. MongoDB serves as the document-oriented NoSQL database, storing data in flexible JSON-like documents that align naturally with JavaScript objects. Express.js provides the web application framework running on Node.js for the backend API, while Angular handles the frontend single-page application with TypeScript support and component-based architecture.

This Docker configuration creates a three-tier architecture where Angular serves the client-side application on port 4200, Express.js handles API requests on port 3000, and MongoDB manages data persistence on the internal network. The stack leverages JavaScript throughout the entire application lifecycle, from database queries using MongoDB's native JavaScript drivers to frontend rendering with Angular's TypeScript-compiled components. The containerized approach isolates each service while maintaining communication through a dedicated Docker network.

Developers building modern web applications, startups requiring rapid prototyping capabilities, and teams seeking a unified JavaScript development experience will find this stack particularly valuable. The MEAN architecture excels in scenarios requiring real-time data updates, content management systems, and applications with evolving schemas where MongoDB's flexibility shines alongside Angular's dynamic frontend capabilities.

## Features

- Document-based data storage with MongoDB's flexible JSON-like schema design
- Real-time data binding between Angular components and Express.js API endpoints
- MongoDB aggregation framework for complex data analytics and reporting
- Angular's component-based architecture with dependency injection and routing
- Express.js middleware ecosystem for authentication, logging, and request processing
- Node.js event-driven architecture enabling high-concurrent connection handling
- MongoDB change streams for real-time application updates and notifications
- Angular CLI integration for rapid component generation and build optimization

## Use cases

- Content management systems requiring flexible document schemas and rich user interfaces
- Social media platforms with real-time feeds, user interactions, and dynamic content
- E-commerce applications managing product catalogs, user profiles, and order processing
- Project management tools with collaborative features and document storage
- Blog platforms and publishing systems with rich text editing and media management
- Dashboard applications displaying real-time analytics and business intelligence
- Learning management systems with course content, user progress tracking, and assessments

## Prerequisites

- Docker and Docker Compose installed with minimum 4GB RAM available for containers
- Angular CLI installed globally for frontend development and build processes
- Node.js and npm knowledge for Express.js backend development and package management
- Ports 3000, 4200, and 27017 available on the host system
- Basic understanding of TypeScript for Angular component development
- MongoDB query syntax familiarity for database operations and aggregation pipelines

## docker-compose.yml

```yaml
services:
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: angular-frontend
    ports:
      - "4200:4200"
    networks:
      - mean

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: express-backend
    environment:
      MONGODB_URI: mongodb://mongodb:27017/${DB_NAME}
    ports:
      - "3000:3000"
    depends_on:
      - mongodb
    networks:
      - mean

  mongodb:
    image: mongo:7
    container_name: mongodb
    volumes:
      - mongodb_data:/data/db
    networks:
      - mean

volumes:
  mongodb_data:

networks:
  mean:
    driver: bridge
```

## .env

```bash
DB_NAME=meanapp
```

## Usage notes

- Docs: https://angular.io/guide/setup-local
- Create frontend/ with ng new (Angular CLI)
- Create backend/ with Express.js API
- Frontend at http://localhost:4200, API at http://localhost:3000
- MongoDB connection: mongodb://mongodb:27017/dbname
- Use HttpClient module for API requests

## Troubleshooting

- Angular build fails with memory errors: Increase Docker container memory limits or add NODE_OPTIONS=--max-old-space-size=4096 to frontend environment
- MongoDB connection refused from Express.js: Ensure backend service depends_on mongodb and uses hostname 'mongodb' instead of 'localhost' in connection string
- CORS errors when Angular calls Express API: Configure Express.js with cors middleware allowing origin http://localhost:4200
- Frontend container exits immediately: Verify Angular CLI is properly installed and package.json contains valid start script for development server
- MongoDB data loss after container restart: Ensure mongodb_data volume is properly mounted to /data/db and has sufficient disk space
- Express.js crashes with module not found: Check that backend Dockerfile includes npm install step and all dependencies are listed in package.json

