A simple yet powerful e-commerce REST API built with NestJS, a progressive Node.js framework for building efficient and scalable server-side applications.
-
User Management
- User registration and authentication
- JWT-based authentication
- Role-based access control (Admin, Customer)
- User profile management
-
Product Management
- CRUD operations for products
- Product categories and subcategories
- Product search and filtering
- Inventory management
- Product image handling
-
Shopping Cart
- Add/remove items from cart
- Update item quantities
- Cart persistence
- Cart calculation (subtotal, tax, total)
-
Order Management
- Place orders from cart
- Order history tracking
- Order status management
- Order confirmation emails
-
Admin Features
- Admin dashboard endpoints
- Product management
- Order management
- User management
- Sales analytics
- Framework: NestJS
- Language: TypeScript
- Database: MongoDB/PostgreSQL/MySQL (configurable)
- ORM: TypeORM/Mongoose
- Authentication: JWT (JSON Web Tokens)
- Validation: class-validator
- Documentation: Swagger/OpenAPI
- Testing: Jest
Before you begin, ensure you have the following installed:
-
Clone the repository
git clone https://github.com/Dilshan97/Nestjs-Ecommerce-Api.git cd Nestjs-Ecommerce-Api
-
Install dependencies
npm install
-
Environment Configuration
Create a
.env
file in the root directory and add the following variables:# Application PORT=3000 NODE_ENV=development # Database DB_HOST=localhost DB_PORT=5432 DB_USERNAME=your_username DB_PASSWORD=your_password DB_DATABASE=ecommerce_db # JWT JWT_SECRET=your_jwt_secret_key JWT_EXPIRES_IN=7d # Email (optional) SMTP_HOST=smtp.gmail.com SMTP_PORT=587 SMTP_USER=your_email@gmail.com SMTP_PASS=your_app_password # File Upload (optional) MAX_FILE_SIZE=5000000 UPLOAD_DEST=./uploads
-
Database Setup
Make sure your database is running and accessible. The application will automatically create the necessary tables/collections on first run.
# Development mode
npm run start:dev
# Production mode
npm run start:prod
# Debug mode
npm run start:debug
The API will be available at http://localhost:3000
Once the application is running, you can access the Swagger API documentation at:
http://localhost:3000/api/docs
# Unit tests
npm run test
# End-to-end tests
npm run test:e2e
# Test coverage
npm run test:cov
src/
├── auth/ # Authentication module
│ ├── guards/ # Auth guards
│ ├── strategies/ # Passport strategies
│ └── dto/ # Data transfer objects
├── users/ # User management module
├── products/ # Product management module
├── categories/ # Category management module
├── cart/ # Shopping cart module
├── orders/ # Order management module
├── admin/ # Admin-specific endpoints
├── common/ # Shared utilities
│ ├── decorators/ # Custom decorators
│ ├── filters/ # Exception filters
│ ├── guards/ # Custom guards
│ ├── interceptors/ # Custom interceptors
│ └── pipes/ # Custom pipes
├── config/ # Configuration files
├── database/ # Database configuration
└── main.ts # Application entry point
The API uses JWT (JSON Web Tokens) for authentication. To access protected endpoints:
- Register a new user or login with existing credentials
- Include the JWT token in the Authorization header:
Authorization: Bearer <your-jwt-token>
- Customer: Can browse products, manage cart, place orders
- Admin: Has all customer permissions plus product/user/order management
POST /auth/register
- User registrationPOST /auth/login
- User loginPOST /auth/refresh
- Refresh JWT token
GET /users/profile
- Get user profilePUT /users/profile
- Update user profileGET /users
- Get all users (Admin only)
GET /products
- Get all products (with filtering & pagination)GET /products/:id
- Get product by IDPOST /products
- Create new product (Admin only)PUT /products/:id
- Update product (Admin only)DELETE /products/:id
- Delete product (Admin only)
GET /categories
- Get all categoriesGET /categories/:id
- Get category by IDPOST /categories
- Create category (Admin only)PUT /categories/:id
- Update category (Admin only)DELETE /categories/:id
- Delete category (Admin only)
GET /cart
- Get user's cartPOST /cart/items
- Add item to cartPUT /cart/items/:id
- Update cart item quantityDELETE /cart/items/:id
- Remove item from cartDELETE /cart
- Clear cart
GET /orders
- Get user's order historyGET /orders/:id
- Get specific orderPOST /orders
- Create new order from cartPUT /orders/:id/status
- Update order status (Admin only)
The application supports various configuration options through environment variables:
- Database: Configure your preferred database connection
- JWT: Set your secret key and token expiration
- File Upload: Configure file upload limits and destination
- Email: Set up SMTP for order confirmations
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Dilshan
- GitHub: @Dilshan97
- NestJS for the amazing framework
- TypeScript for type safety
- The Node.js community for continuous innovation
If you have any questions or issues, please feel free to:
- Open an issue on GitHub
- Contact the author through GitHub
⭐ If you found this project helpful, please give it a star on GitHub!