An open API service indexing awesome lists of open source software.

https://github.com/hassonor/mastering-mern

NodeJS (TS,Redis, Queues, Workers, Jest,Mongoose, Cloudinary, Bunyan, Socket-io, Bull ...)
https://github.com/hassonor/mastering-mern

aws bull bunyan caching cloudinary elasticache jest mongodb mongoose nodejs queues redis socket-io terraform ts workers

Last synced: 2 months ago
JSON representation

NodeJS (TS,Redis, Queues, Workers, Jest,Mongoose, Cloudinary, Bunyan, Socket-io, Bull ...)

Awesome Lists containing this project

README

          

# πŸš€ Mastering MERN Stack - Full-Stack Social Media Platform

![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)
![MongoDB](https://img.shields.io/badge/mongodb-latest-green)
![Express](https://img.shields.io/badge/express-%5E4.18.0-blue)
![React](https://img.shields.io/badge/react-%5E18.0.0-blue)
![Node.js](https://img.shields.io/badge/node-%3E%3D20.x-brightgreen)

**A production-ready social media platform demonstrating the complete MERN stack**

[Features](#-features) β€’ [Quick Start](#-quick-start) β€’ [Architecture](#-architecture) β€’ [Development](#-development)

---

## πŸ“‹ Table of Contents

- [Overview](#-overview)
- [Features](#-features)
- [Tech Stack](#-tech-stack)
- [Architecture](#-architecture)
- [Quick Start](#-quick-start)
- [Project Structure](#-project-structure)
- [Backend API](#-backend-api)
- [Frontend Application](#-frontend-application)
- [Development](#-development)
- [Testing](#-testing)
- [Deployment](#-deployment)
- [Contributing](#-contributing)
- [License](#-license)

---

## 🌟 Overview

**Mastering MERN** is a comprehensive social media platform built from scratch using the MERN stack (MongoDB, Express, React, Node.js). This project demonstrates **production-ready full-stack development** with modern best practices, authentication, real-time features, and responsive design.

### Why This Project?

- βœ… **Complete Full-Stack**: Backend API + Frontend SPA
- βœ… **Production Patterns**: Authentication, validation, error handling
- βœ… **Real-time Features**: Live updates, notifications, chat
- βœ… **Responsive Design**: Mobile-first UI with modern styling
- βœ… **Educational**: Clean code, comprehensive documentation
- βœ… **Scalable Architecture**: Modular design, separation of concerns

---

## ✨ Features

### User Features
- πŸ” **Authentication**: JWT-based auth with refresh tokens
- πŸ‘€ **User Profiles**: Customizable profiles with avatars
- πŸ“ **Posts**: Create, edit, delete posts with rich media
- πŸ’¬ **Comments**: Nested commenting system
- ❀️ **Reactions**: Like, love, and react to posts
- πŸ‘₯ **Social Graph**: Follow/unfollow users, friend requests
- πŸ”” **Notifications**: Real-time notifications
- πŸ’¬ **Messaging**: Direct messaging between users
- πŸ” **Search**: Search users, posts, and hashtags
- πŸ“Š **Feed Algorithm**: Personalized content feed

### Technical Features
- 🎯 **RESTful API**: Clean, documented REST endpoints
- πŸ”„ **Real-time Updates**: WebSocket integration
- πŸ’Ύ **MongoDB**: NoSQL database with Mongoose ODM
- πŸ”’ **Security**: bcrypt passwords, JWT tokens, rate limiting
- πŸ“§ **Email Service**: Email verification and notifications
- πŸ–ΌοΈ **File Upload**: Image/video uploads with Cloudinary
- πŸš€ **Performance**: Redis caching, pagination, lazy loading
- πŸ“± **Responsive UI**: Works on desktop, tablet, and mobile

---

## πŸ›  Tech Stack

### Backend
| Technology | Version | Purpose |
|------------|---------|---------|
| **Node.js** | 20.x | Runtime environment |
| **Express.js** | 4.18+ | Web framework |
| **MongoDB** | Latest | NoSQL database |
| **Mongoose** | 8.x | MongoDB ODM |
| **JWT** | 9.x | Authentication tokens |
| **bcryptjs** | 2.x | Password hashing |
| **Redis** | Latest | Caching and sessions |
| **Bull** | 4.x | Job queues |
| **Socket.io** | 4.x | Real-time communication |

### Frontend
| Technology | Version | Purpose |
|------------|---------|---------|
| **React** | 18.x | UI library |
| **Vite** | 5.x | Build tool |
| **TypeScript** | 5.x | Type safety |
| **React Router** | 6.x | Client-side routing |
| **Axios** | 1.x | HTTP client |
| **React Query** | 5.x | Server state management |
| **Zustand** | 4.x | Client state management |
| **Tailwind CSS** | 3.x | Utility-first CSS |
| **Shadcn/ui** | Latest | UI components |

### DevOps
- **Docker** & **Docker Compose** - Containerization
- **GitHub Actions** - CI/CD pipeline
- **ESLint** & **Prettier** - Code quality

---

## πŸ— Architecture

### High-Level System Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Frontend (React) β”‚
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Pages β”‚ β”‚Componentsβ”‚ β”‚ State (Zustand)β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ React Query (Server State) β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ HTTP/REST
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Backend API (Express) β”‚
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Routes β”‚β†’ β”‚Controllersβ”‚β†’β”‚ Services β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β–Ό β–Ό β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ MongoDB β”‚ β”‚ Redis β”‚ β”‚
β”‚ β”‚ (Mongoose) β”‚ β”‚ (Cache) β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### Backend Architecture (MVC Pattern)

```
backend/
β”œβ”€β”€ routes/ # Express route definitions
β”‚ β”œβ”€β”€ auth.routes.js
β”‚ β”œβ”€β”€ user.routes.js
β”‚ └── post.routes.js
β”‚
β”œβ”€β”€ controllers/ # Route handlers
β”‚ β”œβ”€β”€ auth.controller.js
β”‚ β”œβ”€β”€ user.controller.js
β”‚ └── post.controller.js
β”‚
β”œβ”€β”€ services/ # Business logic
β”‚ β”œβ”€β”€ auth.service.js
β”‚ β”œβ”€β”€ user.service.js
β”‚ └── post.service.js
β”‚
β”œβ”€β”€ models/ # Mongoose schemas
β”‚ β”œβ”€β”€ User.model.js
β”‚ β”œβ”€β”€ Post.model.js
β”‚ └── Comment.model.js
β”‚
β”œβ”€β”€ middleware/ # Express middleware
β”‚ β”œβ”€β”€ auth.middleware.js
β”‚ β”œβ”€β”€ validation.middleware.js
β”‚ └── error.middleware.js
β”‚
└── utils/ # Helper functions
β”œβ”€β”€ email.util.js
β”œβ”€β”€ upload.util.js
└── token.util.js
```

---

## πŸš€ Quick Start

Get the platform running locally in **5 minutes**:

### Prerequisites

- **Node.js** >= 20.x
- **MongoDB** (local or Atlas)
- **Redis** (local or cloud)
- **Git**

### Installation

```bash
# 1. Clone the repository
git clone https://github.com/hassonor/mastering-mern.git
cd mastering-mern

# 2. Install backend dependencies
cd backend
npm install

# 3. Install frontend dependencies
cd ../frontend
npm install

# 4. Set up environment variables
cd ../backend
cp .env.example .env
# Edit .env with your configuration

# MongoDB (local)
MONGODB_URI=mongodb://localhost:27017/mern-social

# Redis (local)
REDIS_URL=redis://localhost:6379

# JWT
JWT_SECRET=your-super-secret-key
JWT_EXPIRES_IN=7d

# Email (optional - for production)
SENDGRID_API_KEY=your-sendgrid-key
SENDGRID_FROM_EMAIL=noreply@example.com

# Cloudinary (optional - for image uploads)
CLOUDINARY_CLOUD_NAME=your-cloud-name
CLOUDINARY_API_KEY=your-api-key
CLOUDINARY_API_SECRET=your-api-secret
```

### Running Locally

#### Option 1: Manual Start

```bash
# Terminal 1 - Start MongoDB (if local)
mongod

# Terminal 2 - Start Redis (if local)
redis-server

# Terminal 3 - Start backend
cd backend
npm run dev

# Terminal 4 - Start frontend
cd frontend
npm run dev
```

#### Option 2: Docker Compose

```bash
# Start all services (MongoDB, Redis, Backend, Frontend)
docker-compose up

# Backend: http://localhost:5000
# Frontend: http://localhost:3000
```

### Access Points

| Service | URL | Description |
|---------|-----|-------------|
| **Frontend** | http://localhost:3000 | React application |
| **Backend API** | http://localhost:5000 | Express API |
| **API Docs** | http://localhost:5000/api-docs | Swagger documentation |
| **MongoDB** | mongodb://localhost:27017 | Database |
| **Redis** | redis://localhost:6379 | Cache |

---

## πŸ“ Project Structure

```
mastering-mern/
β”œβ”€β”€ backend/ # Express backend
β”‚ β”œβ”€β”€ src/
β”‚ β”‚ β”œβ”€β”€ config/ # Configuration files
β”‚ β”‚ β”‚ β”œβ”€β”€ database.js # MongoDB connection
β”‚ β”‚ β”‚ β”œβ”€β”€ redis.js # Redis connection
β”‚ β”‚ β”‚ └── cloudinary.js # File upload config
β”‚ β”‚ β”‚
β”‚ β”‚ β”œβ”€β”€ models/ # Mongoose models
β”‚ β”‚ β”‚ β”œβ”€β”€ User.js
β”‚ β”‚ β”‚ β”œβ”€β”€ Post.js
β”‚ β”‚ β”‚ β”œβ”€β”€ Comment.js
β”‚ β”‚ β”‚ β”œβ”€β”€ Like.js
β”‚ β”‚ β”‚ └── Notification.js
β”‚ β”‚ β”‚
β”‚ β”‚ β”œβ”€β”€ routes/ # API routes
β”‚ β”‚ β”‚ β”œβ”€β”€ auth.routes.js
β”‚ β”‚ β”‚ β”œβ”€β”€ user.routes.js
β”‚ β”‚ β”‚ β”œβ”€β”€ post.routes.js
β”‚ β”‚ β”‚ └── comment.routes.js
β”‚ β”‚ β”‚
β”‚ β”‚ β”œβ”€β”€ controllers/ # Route controllers
β”‚ β”‚ β”œβ”€β”€ services/ # Business logic
β”‚ β”‚ β”œβ”€β”€ middleware/ # Express middleware
β”‚ β”‚ β”œβ”€β”€ utils/ # Helper functions
β”‚ β”‚ β”œβ”€β”€ validators/ # Request validation
β”‚ β”‚ └── app.js # Express app setup
β”‚ β”‚
β”‚ β”œβ”€β”€ tests/ # Backend tests
β”‚ β”œβ”€β”€ package.json
β”‚ └── .env.example
β”‚
β”œβ”€β”€ frontend/ # React frontend
β”‚ β”œβ”€β”€ src/
β”‚ β”‚ β”œβ”€β”€ components/ # Reusable components
β”‚ β”‚ β”‚ β”œβ”€β”€ common/ # Buttons, inputs, etc.
β”‚ β”‚ β”‚ β”œβ”€β”€ layout/ # Navbar, sidebar, footer
β”‚ β”‚ β”‚ β”œβ”€β”€ post/ # Post card, create post
β”‚ β”‚ β”‚ └── user/ # User card, profile
β”‚ β”‚ β”‚
β”‚ β”‚ β”œβ”€β”€ pages/ # Route pages
β”‚ β”‚ β”‚ β”œβ”€β”€ Home.tsx
β”‚ β”‚ β”‚ β”œβ”€β”€ Profile.tsx
β”‚ β”‚ β”‚ β”œβ”€β”€ Login.tsx
β”‚ β”‚ β”‚ └── Register.tsx
β”‚ β”‚ β”‚
β”‚ β”‚ β”œβ”€β”€ hooks/ # Custom React hooks
β”‚ β”‚ β”‚ β”œβ”€β”€ useAuth.ts
β”‚ β”‚ β”‚ β”œβ”€β”€ usePosts.ts
β”‚ β”‚ β”‚ └── useInfiniteScroll.ts
β”‚ β”‚ β”‚
β”‚ β”‚ β”œβ”€β”€ store/ # Zustand stores
β”‚ β”‚ β”‚ β”œβ”€β”€ authStore.ts
β”‚ β”‚ β”‚ └── uiStore.ts
β”‚ β”‚ β”‚
β”‚ β”‚ β”œβ”€β”€ api/ # API client
β”‚ β”‚ β”‚ β”œβ”€β”€ axios.ts # Axios instance
β”‚ β”‚ β”‚ β”œβ”€β”€ auth.api.ts
β”‚ β”‚ β”‚ └── posts.api.ts
β”‚ β”‚ β”‚
β”‚ β”‚ β”œβ”€β”€ types/ # TypeScript types
β”‚ β”‚ β”œβ”€β”€ utils/ # Helper functions
β”‚ β”‚ β”œβ”€β”€ App.tsx
β”‚ β”‚ └── main.tsx
β”‚ β”‚
β”‚ β”œβ”€β”€ package.json
β”‚ └── vite.config.ts
β”‚
β”œβ”€β”€ .github/
β”‚ └── workflows/ # CI/CD pipelines
β”‚ └── ci.yml
β”‚
β”œβ”€β”€ docker-compose.yml # Docker services
└── README.md
```

---

## πŸ”§ Backend API

### Key Endpoints

#### Authentication
```bash
POST /api/auth/register # Register new user
POST /api/auth/login # Login user
POST /api/auth/refresh # Refresh access token
GET /api/auth/me # Get current user
POST /api/auth/logout # Logout user
```

#### Users
```bash
GET /api/users # Get all users (paginated)
GET /api/users/:id # Get user by ID
PUT /api/users/:id # Update user profile
DELETE /api/users/:id # Delete user
POST /api/users/:id/follow # Follow user
DELETE /api/users/:id/follow # Unfollow user
```

#### Posts
```bash
GET /api/posts # Get feed (paginated)
POST /api/posts # Create post
GET /api/posts/:id # Get post by ID
PUT /api/posts/:id # Update post
DELETE /api/posts/:id # Delete post
POST /api/posts/:id/like # Like post
DELETE /api/posts/:id/like # Unlike post
```

#### Comments
```bash
GET /api/posts/:postId/comments # Get comments
POST /api/posts/:postId/comments # Add comment
PUT /api/comments/:id # Update comment
DELETE /api/comments/:id # Delete comment
```

---

## πŸ’» Frontend Application

### Key Features

#### Authentication Flow
```tsx
// Login component with form validation
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
import { useAuthStore } from '@/store/authStore';

const loginSchema = z.object({
email: z.string().email(),
password: z.string().min(6)
});

function Login() {
const login = useAuthStore(state => state.login);
const { register, handleSubmit, formState: { errors } } = useForm({
resolver: zodResolver(loginSchema)
});

const onSubmit = async (data) => {
await login(data.email, data.password);
};

return ...;
}
```

#### Data Fetching with React Query
```tsx
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { getPosts, createPost } from '@/api/posts.api';

function Feed() {
const queryClient = useQueryClient();

const { data, isLoading } = useQuery({
queryKey: ['posts'],
queryFn: getPosts
});

const createMutation = useMutation({
mutationFn: createPost,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['posts'] });
}
});

return

...
;
}
```

---

## πŸ§ͺ Testing

### Backend Tests

```bash
# Run backend tests
cd backend
npm test

# Watch mode
npm run test:watch

# Coverage
npm run test:coverage
```

### Frontend Tests

```bash
# Run frontend tests
cd frontend
npm test

# E2E tests (Playwright)
npm run test:e2e
```

---

## 🚒 Deployment

### Backend Deployment (Railway/Render)

```bash
# Build for production
npm run build

# Start production server
npm start
```

### Frontend Deployment (Vercel/Netlify)

```bash
# Build for production
npm run build

# Preview build locally
npm run preview
```

### Docker Deployment

```bash
# Build and run all services
docker-compose up --build -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down
```

---

## 🀝 Contributing

Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.

---

## πŸ“„ License

This project is licensed under the **MIT License** - see the [LICENSE](LICENSE) file for details.

---

**Made with ❀️ by [Or Hasson](https://github.com/hassonor)**

⭐ Star this repo if you're learning the MERN stack!