Backend API for Clothica built with Node.js, Express and MongoDB.
- π JWT Authentication (Access & Refresh Tokens)
- π‘οΈ Security (Helmet, CORS, Rate Limiting)
- β Request Validation (Celebrate/Joi)
- π API Documentation (Swagger)
- ποΈ Functional Architecture
- π Centralized Error Handling
- 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
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
- Node.js (v14 or higher)
- MongoDB (local or cloud instance)
- npm or yarn
- Clone the repository:
git clone <repository-url>
cd clothica-shop-backend- Install dependencies:
npm install- Create environment file:
cp .env.example .env- Configure environment variables in
.env.
Development mode with auto-restart:
npm run devProduction mode:
npm startOnce the server is running, access the Swagger documentation at:
/api-docs
POST /api/auth/registerPOST /api/auth/loginPOST /api/auth/logoutPOST /api/auth/refreshPOST /api/auth/request-password-resetPOST /api/auth/reset-password
GET /api/users/profilePATCH /api/users/profileDELETE /api/users/profileGET /api/users/profile/telegram-link
GET /api/categoriesGET /api/categories/:idPOST /api/categoriesPATCH /api/categories/:idDELETE /api/categories/:idPATCH /api/categories/:id/img
GET /api/goodsGET /api/goods/:idPOST /api/goodsPATCH /api/goods/:idDELETE /api/goods/:id
GET /api/ordersPOST /api/ordersPATCH /api/orders/:id/status
GET /api/feedbacksPOST /api/feedbacks
POST /api/subscriptions
Authentication endpoints (/register and /login) are rate-limited to 10 requests per 15 minutes per IP address to prevent brute-force attacks.
- Passwords are hashed using bcrypt with salt rounds
- Minimum password length: 8 characters
- Maximum password length: 128 characters
- Access tokens expire in 15 minutes
- Refresh tokens expire in 1 day
- Tokens are verified on protected routes
- Helmet middleware sets secure HTTP headers
- CORS configured for cross-origin requests
- name: Required, string, max 32 characters
- phone: Required, string, max 13 characters
- password: Required, string, min 8 characters, max 128 characters
- phone: Required, string, max 13 characters
- password: Required, string
The API uses centralized error handling with consistent error responses:
{
"success": false,
"message": "Error message",
"errors": [...] // Optional validation errors
}Common HTTP status codes:
200- Success201- Created400- Bad Request (validation errors)401- Unauthorized404- Not Found409- Conflict (duplicate resource)429- Too Many Requests (rate limit exceeded)500- Internal Server Error
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
ctrlWrapperutility