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.
- Host: GitHub
- URL: https://github.com/jayakrishnan-mk/nestjs-mongodb--boilerplate
- Owner: Jayakrishnan-mk
- Created: 2025-08-02T12:48:17.000Z (12 months ago)
- Default Branch: master
- Last Pushed: 2025-08-02T13:14:02.000Z (12 months ago)
- Last Synced: 2025-08-11T15:30:52.937Z (12 months ago)
- Topics: file-upload, jwt, mongodb, mongoose, nestjs, redis
- Language: TypeScript
- Homepage:
- Size: 29.3 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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! ๐**