Skip to content

neuropunk87/clothica-shop-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

140 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Clothica Backend API

Backend API for Clothica built with Node.js, Express and MongoDB.

Features

  • πŸ” JWT Authentication (Access & Refresh Tokens)
  • πŸ›‘οΈ Security (Helmet, CORS, Rate Limiting)
  • βœ… Request Validation (Celebrate/Joi)
  • πŸ“š API Documentation (Swagger)
  • πŸ—οΈ Functional Architecture
  • πŸ”„ Centralized Error Handling

Tech Stack

  • Runtime: Node.js
  • Framework: Express.js
  • Database: MongoDB with Mongoose
  • Authentication: JWT (jsonwebtoken)
  • Password Hashing: bcrypt
  • Validation: Celebrate (Joi wrapper)
  • Security: Helmet, CORS, express-rate-limit
  • Documentation: Swagger (swagger-jsdoc, swagger-ui-express)
  • Module System: ES Modules (type: "module")
  • Architecture: Functional Programming

Project Structure

clothica-shop-backend/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ server.js
β”‚   β”œβ”€β”€ admin/
β”‚   β”‚   β”œβ”€β”€ admin.config.js
β”‚   β”‚   β”œβ”€β”€ auth.js
β”‚   β”‚   └── resources.js
β”‚   β”œβ”€β”€ constants/
β”‚   β”‚   β”œβ”€β”€ colors.js
β”‚   β”‚   β”œβ”€β”€ orderStatuses.js
β”‚   β”‚   └── time.js
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   β”œβ”€β”€ authController.js
β”‚   β”‚   β”œβ”€β”€ userController.js
β”‚   β”‚   β”œβ”€β”€ categoryController.js
β”‚   β”‚   β”œβ”€β”€ goodController.js
β”‚   β”‚   β”œβ”€β”€ orderController.js
β”‚   β”‚   β”œβ”€β”€ feedbackController.js
β”‚   β”‚   └── subscriptionController.js
β”‚   β”œβ”€β”€ db/
β”‚   β”‚   └── connectMongoDB.js
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   β”œβ”€β”€ authenticate.js
β”‚   β”‚   β”œβ”€β”€ logger.js
β”‚   β”‚   β”œβ”€β”€ errorHandler.js
β”‚   β”‚   β”œβ”€β”€ notFoundHandler.js
β”‚   β”‚   β”œβ”€β”€ rateLimitAuth.js
β”‚   β”‚   β”œβ”€β”€ rateLimitSearch.js
β”‚   β”‚   β”œβ”€β”€ requireAdmin.js
β”‚   β”‚   β”œβ”€β”€ processCategoryFilter.js
β”‚   β”‚   └── multer.js
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ user.js
β”‚   β”‚   β”œβ”€β”€ session.js
β”‚   β”‚   β”œβ”€β”€ category.js
β”‚   β”‚   β”œβ”€β”€ good.js
β”‚   β”‚   β”œβ”€β”€ order.js
β”‚   β”‚   β”œβ”€β”€ feedback.js
β”‚   β”‚   β”œβ”€β”€ subscription.js
β”‚   β”‚   └── counter.js
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ authRoutes.js
β”‚   β”‚   β”œβ”€β”€ userRoutes.js
β”‚   β”‚   β”œβ”€β”€ categoryRoutes.js
β”‚   β”‚   β”œβ”€β”€ goodRoutes.js
β”‚   β”‚   β”œβ”€β”€ orderRoutes.js
β”‚   β”‚   β”œβ”€β”€ feedbackRoutes.js
β”‚   β”‚   └── subscriptionRoutes.js
β”‚   β”œβ”€β”€ seeds/
β”‚   β”‚   └── setCounter.js
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ auth.js
β”‚   β”‚   └── telegram.js
β”‚   β”œβ”€β”€ templates/
β”‚   β”‚   └── reset-password-email.html
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”œβ”€β”€ ctrlWrapper.js
β”‚   β”‚   β”œβ”€β”€ modifyFileToCloudinary.js
β”‚   β”‚   └── sendMail.js
β”‚   β”œβ”€β”€ validations/
β”‚   β”‚   β”œβ”€β”€ authValidation.js
β”‚   β”‚   β”œβ”€β”€ categoriesValidation.js
β”‚   β”‚   β”œβ”€β”€ goodsValidation.js
β”‚   β”‚   β”œβ”€β”€ ordersValidation.js
β”‚   β”‚   β”œβ”€β”€ feedbacksValidation.js
β”‚   └── └── subscriptionsValidation.js
β”œβ”€β”€ config/
β”‚   └── swagger.js
β”œβ”€β”€ .env.example
β”œβ”€β”€ .gitignore
β”œβ”€β”€ package.json
└── README.md

Getting Started

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB (local or cloud instance)
  • npm or yarn

Installation

  1. Clone the repository:
git clone <repository-url>
cd clothica-shop-backend
  1. Install dependencies:
npm install
  1. Create environment file:
cp .env.example .env
  1. Configure environment variables in .env.

Running the Application

Development mode with auto-restart:

npm run dev

Production mode:

npm start

API Documentation

Once the server is running, access the Swagger documentation at:

/api-docs

API Endpoints

Authentication

  • POST /api/auth/register
  • POST /api/auth/login
  • POST /api/auth/logout
  • POST /api/auth/refresh
  • POST /api/auth/request-password-reset
  • POST /api/auth/reset-password

Users (Protected)

  • GET /api/users/profile
  • PATCH /api/users/profile
  • DELETE /api/users/profile
  • GET /api/users/profile/telegram-link

Categories

  • GET /api/categories
  • GET /api/categories/:id
  • POST /api/categories
  • PATCH /api/categories/:id
  • DELETE /api/categories/:id
  • PATCH /api/categories/:id/img

Goods

  • GET /api/goods
  • GET /api/goods/:id
  • POST /api/goods
  • PATCH /api/goods/:id
  • DELETE /api/goods/:id

Orders (All Protected)

  • GET /api/orders
  • POST /api/orders
  • PATCH /api/orders/:id/status

Feedbacks

  • GET /api/feedbacks
  • POST /api/feedbacks

Subscriptions

  • POST /api/subscriptions

Security Features

Rate Limiting

Authentication endpoints (/register and /login) are rate-limited to 10 requests per 15 minutes per IP address to prevent brute-force attacks.

Password Security

  • Passwords are hashed using bcrypt with salt rounds
  • Minimum password length: 8 characters
  • Maximum password length: 128 characters

JWT Authentication

  • Access tokens expire in 15 minutes
  • Refresh tokens expire in 1 day
  • Tokens are verified on protected routes

HTTP Security

  • Helmet middleware sets secure HTTP headers
  • CORS configured for cross-origin requests

Validation Rules

Registration

  • name: Required, string, max 32 characters
  • phone: Required, string, max 13 characters
  • password: Required, string, min 8 characters, max 128 characters

Login

  • phone: Required, string, max 13 characters
  • password: Required, string

Error Handling

The API uses centralized error handling with consistent error responses:

{
  "success": false,
  "message": "Error message",
  "errors": [...]  // Optional validation errors
}

Common HTTP status codes:

  • 200 - Success
  • 201 - Created
  • 400 - Bad Request (validation errors)
  • 401 - Unauthorized
  • 404 - Not Found
  • 409 - Conflict (duplicate resource)
  • 429 - Too Many Requests (rate limit exceeded)
  • 500 - Internal Server Error

Development Notes

Architecture

This project follows a functional programming approach:

  • Controllers: Pure functions that handle requests and responses
  • Services: Pure functions that contain business logic
  • Models: Mongoose schemas with named exports
  • Middleware: Functions for request processing
  • Error Handling: Centralized with ctrlWrapper utility