A robust and scalable backend for an e-commerce platform built with NestJS. This project provides a complete set of features for managing products, users, orders, and authentication, all within a modern, modular architecture. The application is fully containerized with Docker for easy setup and deployment.
- RESTful API: A well-structured and documented API for all e-commerce functionalities.
- Authentication: Secure JWT-based authentication with Access and Refresh Token strategies using Passport.js.
- Role-Based Access Control (RBAC): Guards to restrict access to certain endpoints based on user roles (e.g., admin).
- Product Management: Full CRUD operations for products, categories, and subcategories.
- Image Uploads: Handles product image and thumbnail uploads, with on-the-fly image processing and optimization using
multerandsharp. - Order Management: Logic for creating and managing user orders.
- User Management: Endpoints for managing user data.
- Configuration Management: Centralized configuration using
@nestjs/config. - Database Integration: Uses Mongoose for elegant MongoDB object modeling.
- Dockerized Environment: Comes with a multi-stage
Dockerfileand adocker-compose.ymlfor a one-command setup of both the application and a local MongoDB database.
- Framework: NestJS
- Language: TypeScript
- Database: MongoDB with Mongoose
- Authentication: Passport.js (jwt, local strategies)
- Image Handling: Multer & Sharp
- Containerization: Docker & Docker Compose
- Linting/Formatting: ESLint & Prettier
There are two ways to get the application running: using Docker (recommended for a quick and consistent setup) or running it locally on your machine.
- Node.js (v18 or higher)
- npm or yarn
- Docker Desktop (for the Docker setup)
- A
.envfile (see the Environment Variables section below)
This is the fastest and most reliable way to get started. It will set up both the application and a MongoDB database container that are configured to work together.
-
Clone the repository:
git https://github.com/MahdiPourkeshavarz/nestjs-ecommerce.git cd <your-project-directory>
-
Create an environment file: Create a file named
.envin the root of the project and fill it with your configuration variables. You can use the.env.examplesection below as a template. -
Build and run the containers: Run the following single command in your terminal.
docker-compose up --build
- The
--buildflag is only needed the first time or when you make changes to theDockerfile. - To run in the background, use
docker-compose up -d.
- The
The API will be available at http://localhost:8000 (or whichever port you configured).
-
Clone the repository:
git https://github.com/MahdiPourkeshavarz/nestjs-ecommerce.git cd <your-project-directory>
-
Install dependencies:
npm install
-
Set up the environment: Create a
.envfile in the root directory and add the necessary configuration. Make sure yourDATABASE_URLpoints to a running MongoDB instance. -
Run the application:
npm run start:dev
The application will start in watch mode, automatically restarting on file changes. The API will be available at
http://localhost:8000.
Create a .env file in the project root and add the following variables. Do not commit this file to version control.
# --- Application Configuration ---
PORT=8000
HOST=127.0.0.1
# --- Database ---
# Use your MongoDB Atlas connection string for production/cloud dev
# For local Docker setup, this will be overridden by docker-compose.yml
DATABASE_URL=mongodb+srv://<user>:<password>@<cluster-url>/<db-name>?retryWrites=true&w=majority
# --- JWT Authentication ---
JWT_ACCESS_TOKEN_SECRET=your_super_secret_key_for_access_tokens
JWT_ACCESS_TOKEN_EXPIRES_IN=15m
JWT_REFRESH_TOKEN_SECRET=your_even_more_secret_key_for_refresh_tokens
JWT_REFRESH_TOKEN_EXPIRES_IN=7d
# --- Default Admin User (for initial seeding, if applicable) ---
ADMIN_FIRSTNAME=admin
ADMIN_LASTNAME=admin
ADMIN_USERNAME=admin
ADMIN_PASSWORD=admin1234
ADMIN_PHONE_NUMBER=09123456789
ADMIN_ADDRESS=Some AddressThe project structure is organized by feature modules, promoting modularity and separation of concerns.
/
├── public/ # Static files (e.g., uploaded images)
├── src/
│ ├── auth/ # Authentication logic (controllers, services, strategies, guards)
│ ├── categories/ # Categories feature module
│ ├── common/ # Common utilities or decorators
│ ├── config/ # Application configuration (multer.config.ts)
│ ├── database/ # Database related files
│ ├── orders/ # Orders feature module
│ ├── products/ # Products feature module (includes image-processing.service.ts)
│ ├── subcategories/ # Subcategories feature module
│ ├── users/ # Users feature module
│ ├── app.module.ts # Root application module
│ └── main.ts # Application entry point
├── .dockerignore
├── .env # (You create this) Environment variables
├── .gitignore
├── docker-compose.yml # Docker Compose configuration
├── Dockerfile # Multi-stage Docker build instructions
└── package.json
This project is set up to easily integrate with Swagger for auto-generated, interactive API documentation. To enable it:
- Install Swagger dependencies:
npm install @nestjs/swagger
- In
src/main.ts, add the Swagger setup code.
Once configured, the interactive API documentation will be available at http://localhost:8000/api.
This project is licensed under the MIT License. See the LICENSE file for details.