Backend API for the Project Stride e-commerce application. This server handles authentication, product listing, cart management, order placement, and admin operations such as product management, user blocking, and order tracking.
- User registration, login, and logout
- Cookie-based JWT authentication
- Public product listing with pagination and filtering
- Cart management for authenticated users
- Order placement and order history
- Admin login and dashboard
- Admin product create, update, and soft delete
- Admin user block/unblock
- Cloudinary image upload for product images
- Node.js
- Express
- MongoDB with Mongoose
- JWT authentication
- Joi validation
- Multer + Cloudinary for uploads
Back End/
|- config/
|- controllers/
|- middleware/
|- models/
|- routes/
|- utils/
|- validation/
`- server.js
Create a .env file in the project root and add:
PORT=5000
MONGO_URL=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret
FRONTEND_URL=http://localhost:3000
CLOUD_NAME=your_cloudinary_cloud_name
API_KEY=your_cloudinary_api_key
API_SECRET=your_cloudinary_api_secretnpm installDevelopment:
npm run devProduction:
npm start- Login stores a JWT in an HTTP-only cookie named
token - Protected routes depend on that cookie being sent with the request
- Frontend requests should use credentials
- Admin-only routes require a logged-in user with
role: "admin"
http://localhost:<PORT>/api
Base route: /api/auth
POST /register- Register a new userPOST /login- Login userDELETE /logout- Logout user
Example register body:
{
"username": "john",
"email": "john@example.com",
"password": "123456"
}Base route: /api/products
GET /- Get all productsGET /:id- Get single product
Supported query params:
pagelimitkeywordbrandcategory
Base route: /api/cart
POST /- Add product to cartGET /- Get current user cartPATCH /- Update cart item quantityDELETE /- Remove item from cart
Example cart body:
{
"productId": "product_id_here",
"size": 42,
"quantity": 1
}Base route: /api/order
POST /- Place order from cart itemsGET /- Get logged-in user orders
Example order body:
{
"address": {
"name": "John Doe",
"phone": "1234567890",
"city": "Kochi",
"state": "Kerala",
"pincode": "682001",
"addressLine": "Street address"
},
"payment": "COD"
}Base route: /api/admin
POST /adminlogin- Admin loginGET /dashboard- Dashboard statsGET /users- Get all usersPATCH /users/:id- Block or unblock a userGET /orders- Get all ordersPATCH /orders/:id- Update order statusGET /products- Get all products for admin panelPOST /products- Create productPUT /products/:id- Update productDELETE /products/:id- Soft delete product
Admin product create and update endpoints use multipart/form-data.
- Image field name:
image - Max file size:
3MB - Allowed formats:
jpg,jpeg,png,webp
Example product fields:
brandnamecategorypricesizesdescriptionimage
usernameemailpasswordprofileImgroleisBlockedisDeleted
brandnamecategorypricesizesdescriptionimageisDeleted
userIdproductIdsizequantity
userIdproductsorderIdtotalPricepaymentMethodshippingDetailsstatus
Current Joi validation is applied for:
- User registration
- User login
Rules include:
usernameminimum 3 characters- Valid email format
passwordminimum 6 characters
- Deleted products are soft deleted using
isDeleted: true - Product listing excludes soft-deleted products
- Blocked users cannot access protected routes
- Pagination defaults to
page=1andlimit=12 - CORS is configured using
FRONTEND_URL
- Add API documentation with Postman or Swagger
- Add refresh token support
- Improve cookie security for production with
secure: true - Add centralized error handling
- Add tests