# MERN Stack

> MongoDB, Express, React, Node.js full-stack JavaScript setup.

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

## Overview

MongoDB is a NoSQL document database that revolutionized web development by storing data in flexible, JSON-like documents rather than rigid table structures. Launched in 2009, MongoDB became the foundation for modern JavaScript applications by providing schema flexibility, horizontal scaling capabilities, and native JSON support that aligns perfectly with JavaScript's object-oriented nature.

The MERN stack combines MongoDB's document storage with Express.js (Node.js web framework), React (frontend library), and Node.js runtime to create a full-JavaScript development environment. This configuration eliminates context switching between different programming languages and data formats, as JavaScript objects flow naturally from React components through Express APIs to MongoDB documents. The stack leverages MongoDB's aggregation framework for complex queries, Express middleware for API logic, React's component architecture for dynamic UIs, and Node.js's event-driven model for handling concurrent requests.

This stack is ideal for startups building MVP applications, development teams wanting rapid prototyping capabilities, and organizations creating content-heavy applications with evolving data requirements. The MERN combination excels in scenarios requiring real-time features, user-generated content, and applications where data structures frequently change during development cycles.

## Features

- Flexible document schema allowing data structure evolution without migrations
- MongoDB aggregation framework for complex data analytics and reporting
- React hot-reloading for instant UI updates during development
- Express.js middleware ecosystem with thousands of available plugins
- Node.js non-blocking I/O for handling thousands of concurrent connections
- MongoDB change streams for real-time data synchronization across components
- Single JavaScript language across entire application stack
- MongoDB horizontal sharding for scaling beyond single server limitations

## Use cases

- Social media platforms requiring user profiles, posts, and real-time interactions
- E-commerce applications with product catalogs, user reviews, and shopping carts
- Content management systems with dynamic article structures and media handling
- Real-time collaboration tools like project management or document editing platforms
- IoT dashboards collecting and visualizing sensor data with flexible schemas
- Startup MVPs requiring rapid feature development and frequent data model changes
- Educational platforms with courses, user progress tracking, and interactive content

## Prerequisites

- Docker Engine 20.10+ and Docker Compose V2 installed on host system
- Minimum 4GB RAM available (MongoDB requires 2GB+, Node.js applications need 512MB each)
- Ports 3000, 5000, and 27017 available on host machine
- Basic JavaScript/ES6 knowledge for customizing React and Express components
- Understanding of RESTful API design principles for Express.js backend development
- Familiarity with MongoDB query syntax and document-oriented data modeling

## docker-compose.yml

```yaml
services:
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: frontend
    ports:
      - "3000:3000"
    networks:
      - mern

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

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

volumes:
  mongodb_data:

networks:
  mern:
    driver: bridge
```

## .env

```bash
DB_NAME=mernapp
```

## Usage notes

- Docs: https://www.mongodb.com/mern-stack
- Create frontend/ with Create React App or Vite
- Create backend/ with Express.js API
- Frontend at http://localhost:3000, API at http://localhost:5000
- MongoDB connection: mongodb://mongodb:27017/dbname
- Add hot-reload with volume mounts in development

## Troubleshooting

- MongoNetworkError connection refused: Ensure MongoDB container is running and backend depends_on is properly configured
- React app shows blank page: Check browser console for JavaScript errors and verify frontend build completed successfully
- CORS errors when calling API: Add cors middleware to Express.js backend or configure proxy in React development server
- MongoDB container exits with code 125: Increase Docker Desktop memory allocation to at least 4GB for MongoDB operations
- Hot reload not working in development: Add volume mounts for ./frontend/src and ./backend directories to docker-compose.yml
- Express API returns 404 for all routes: Verify Express router setup and ensure routes are properly mounted in app.js

