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

https://github.com/jayakrishnan-mk/nestjs-mongodb--boilerplate

A comprehensive NestJS boilerplate with MongoDB, JWT authentication, file upload, Redis caching, and rate limiting.
https://github.com/jayakrishnan-mk/nestjs-mongodb--boilerplate

file-upload jwt mongodb mongoose nestjs redis

Last synced: 4 months ago
JSON representation

A comprehensive NestJS boilerplate with MongoDB, JWT authentication, file upload, Redis caching, and rate limiting.

Awesome Lists containing this project

README

          

# NestJS Boilerplate

A comprehensive NestJS boilerplate with MongoDB, JWT authentication, file upload, Redis caching, and rate limiting.

## ๐Ÿš€ Features

- **NestJS** - Progressive Node.js framework
- **MongoDB** - NoSQL database with Mongoose ODM
- **JWT Authentication** - Secure token-based authentication
- **File Upload** - Image upload with validation
- **Redis Caching** - High-performance caching
- **Rate Limiting** - API rate limiting protection
- **Docker** - Containerized development and deployment
- **Environment Configuration** - Separate dev/prod configs
- **Validation** - Request validation with class-validator
- **CORS** - Cross-origin resource sharing enabled

## ๐Ÿ“‹ Prerequisites

- Node.js (v18 or higher)
- Docker and Docker Compose (for containerized setup)
- MongoDB (if running locally)
- Redis (if running locally)

## ๐Ÿ› ๏ธ Installation

### Option 1: Local Development

1. **Clone and install dependencies:**
```bash
git clone https://github.com/Jayakrishnan-mk/NestJs-MongoDB--Boilerplate.git
cd project-name
npm install
```

2. **Set up environment:**
```bash
# Copy environment files
cp env.dev .env.dev
cp env.prod .env.prod

# Edit the environment files with your configuration
```

3. **Start MongoDB and Redis:**
```bash
# Using Docker (recommended)
docker run -d --name mongodb -p 27017:27017 mongo:6.0
docker run -d --name redis -p 6379:6379 redis:7-alpine

# Or install locally
```

4. **Run the application:**
```bash
# Development
npm run start:dev

# Production
npm run build
npm run start:prod
```

### Option 2: Docker Setup (Recommended)

1. **Start all services:**
```bash
docker-compose up -d
```

2. **View logs:**
```bash
docker-compose logs -f app
```

3. **Stop services:**
```bash
docker-compose down
```

## ๐Ÿ”ง Configuration

### Environment Variables

Create `.env.dev` and `.env.prod` files:

```env
# Database Configuration
MONGODB_URI=mongodb://localhost:27017/project-name-dev

# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
JWT_EXPIRES_IN=7d

# Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=

# Rate Limiting
THROTTLE_TTL=60
THROTTLE_LIMIT=100

# File Upload
MAX_FILE_SIZE=5242880
UPLOAD_DEST=./uploads

# Server Configuration
PORT=3000
NODE_ENV=development
```

## ๐Ÿ“š API Documentation

### Authentication Endpoints

#### Register User
```http
POST /api/auth/register
Content-Type: application/json

{
"email": "user@example.com",
"password": "password123",
"firstName": "John",
"lastName": "Doe"
}
```

#### Login User
```http
POST /api/auth/login
Content-Type: application/json

{
"email": "user@example.com",
"password": "password123"
}
```

### User Endpoints (Protected)

#### Get User Profile
```http
GET /api/users/profile
Authorization: Bearer
```

#### Get All Users
```http
GET /api/users
Authorization: Bearer
```

#### Get User by ID
```http
GET /api/users/:id
Authorization: Bearer
```

#### Update User
```http
PATCH /api/users/:id
Authorization: Bearer
Content-Type: application/json

{
"firstName": "Updated Name"
}
```

#### Delete User
```http
DELETE /api/users/:id
Authorization: Bearer
```

### File Upload Endpoints (Protected)

#### Upload Image
```http
POST /api/upload/image
Authorization: Bearer
Content-Type: multipart/form-data

file:
```

### Health Check

#### Health Status
```http
GET /api/health
```

## ๐Ÿ—๏ธ Project Structure

```
src/
โ”œโ”€โ”€ auth/ # Authentication module
โ”‚ โ”œโ”€โ”€ guards/ # JWT guards
โ”‚ โ”œโ”€โ”€ strategies/ # Passport strategies
โ”‚ โ”œโ”€โ”€ auth.controller.ts
โ”‚ โ”œโ”€โ”€ auth.service.ts
โ”‚ โ””โ”€โ”€ auth.module.ts
โ”œโ”€โ”€ users/ # Users module
โ”‚ โ”œโ”€โ”€ dto/ # Data transfer objects
โ”‚ โ”œโ”€โ”€ schemas/ # Mongoose schemas
โ”‚ โ”œโ”€โ”€ users.controller.ts
โ”‚ โ”œโ”€โ”€ users.service.ts
โ”‚ โ””โ”€โ”€ users.module.ts
โ”œโ”€โ”€ upload/ # File upload module
โ”‚ โ”œโ”€โ”€ upload.controller.ts
โ”‚ โ””โ”€โ”€ upload.module.ts
โ”œโ”€โ”€ cache/ # Redis cache module
โ”‚ โ””โ”€โ”€ cache.module.ts
โ”œโ”€โ”€ config/ # Configuration
โ”‚ โ””โ”€โ”€ configuration.ts
โ”œโ”€โ”€ database/ # Database configuration
โ”‚ โ””โ”€โ”€ database.module.ts
โ”œโ”€โ”€ app.controller.ts
โ”œโ”€โ”€ app.service.ts
โ”œโ”€โ”€ app.module.ts
โ””โ”€โ”€ main.ts
```

## ๐Ÿงช Testing

```bash
# Unit tests
npm run test

# e2e tests
npm run test:e2e

# Test coverage
npm run test:cov
```

## ๐Ÿ“ฆ Available Scripts

- `npm run start:dev` - Start development server
- `npm run start:prod` - Start production server
- `npm run build` - Build the application
- `npm run test` - Run unit tests
- `npm run test:e2e` - Run e2e tests
- `npm run test:cov` - Run test coverage

## ๐Ÿ”’ Security Features

- JWT token authentication
- Password hashing with bcrypt
- Rate limiting protection
- Input validation and sanitization
- CORS configuration
- Environment variable protection

## ๐Ÿš€ Deployment

### Docker Deployment

1. **Build and run:**
```bash
docker-compose -f docker-compose.yml up -d
```

2. **Production deployment:**
```bash
# Set NODE_ENV=production in environment
docker-compose -f docker-compose.prod.yml up -d
```

### Manual Deployment

1. **Build the application:**
```bash
npm run build
```

2. **Set production environment:**
```bash
export NODE_ENV=production
```

3. **Start the application:**
```bash
npm run start:prod
```

## ๐Ÿค Contributing

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## ๐Ÿ“„ License

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

## ๐Ÿ†˜ Support

If you encounter any issues or have questions, please:

1. Check the [Issues](https://github.com/your-repo/issues) page
2. Create a new issue with detailed information
3. Contact the development team

---

**Happy Coding! ๐ŸŽ‰**