A complete solution for an admin panel product page with Cloudinary image storage and large file support (up to 20MB per image).
- Upload product images larger than 5MB (up to 20MB per image)
- Use Cloudinary for storing images
- Save product details (title, price, description, category, images array) into MongoDB
- Backend in Node.js + Express with Mongoose
- File uploads via multer + Cloudinary SDK
- Frontend in React with responsive product form
- Image preview, upload progress, error handling
- Show newly added product immediately in frontend product list
- Fixed issues with file size limits and error messages
- Node.js (v14 or higher)
- MongoDB database
- Cloudinary account
PORT=5000
MONGODB_URI="your_mongodb_connection_string"
JWT_SECRET=your_jwt_secret
JWT_EXPIRES_IN=7d
CLOUDINARY_CLOUD_NAME=your_cloudinary_cloud_name
CLOUDINARY_API_KEY=your_cloudinary_api_key
CLOUDINARY_API_SECRET=your_cloudinary_api_secret- Clone the repository:
git clone <repository-url>
cd mern-ecommerce-2024-master- Install all dependencies:
npm run install:allTo run both frontend and backend together:
npm run devThis will start:
- Backend server on http://localhost:5000
- Frontend development server on http://localhost:5173
cd server
npm startThe server will start on http://localhost:5000
cd client
npm run devThe client will start on http://localhost:5173
- Update the
.envfile in theserverdirectory with production values - Deploy to your preferred platform (Render, Railway, or Heroku)
- Set the start command to
npm start - Add all environment variables to your deployment platform
- Update
API_BASE_URLinclient/src/config/api.jswith your deployed backend URL - Build the frontend:
cd client npm run build - Deploy the
client/distfolder to Vercel or Netlify - Set the build command to
npm run buildand output directory todist
For detailed deployment instructions, see DEPLOYMENT_GUIDE.md
- Access the admin panel at http://localhost:5173/admin
- Log in with admin credentials
- Navigate to the Products section
- Click "Add New Product" to open the product form
- Fill in product details:
- Upload a main image (required, max 20MB)
- Upload up to 10 sub-images (optional, max 20MB each)
- Enter product title, description, category, brand, price
- Set sale price (optional)
- Configure color options
- Click "Save Product" to upload images to Cloudinary and save product to MongoDB
- Uses multer with memory storage for handling file uploads
- Integrates with Cloudinary SDK for image storage
- Implements 20MB file size limit
- Stores product data in MongoDB using Mongoose
- RESTful API endpoints for product management
- Responsive product form with image preview
- Drag and drop support for image uploads
- Upload progress indicators
- Comprehensive form validation
- Error handling with user-friendly messages
- Real-time product listing after saving
GET /api/admin/products/get- Fetch all productsPOST /api/admin/products/add- Add new product with imagesPUT /api/admin/products/edit/:id- Edit existing productDELETE /api/admin/products/delete/:id- Delete product
To test the product upload functionality, you can use the admin panel UI or create a test script like the one below:
// test-product-upload.js
const axios = require('axios');
const fs = require('fs');
async function testProductUpload() {
try {
const formData = new FormData();
// Add product details
formData.append('title', 'Test Product');
formData.append('description', 'Test product description');
formData.append('category', 'ethnic-wear');
formData.append('brand', 'Test Brand');
formData.append('price', '99.99');
formData.append('colors', JSON.stringify(['#FF0000']));
formData.append('sizes', JSON.stringify(['M', 'L']));
// Add a test image file
// formData.append('mainImage', fs.createReadStream('path/to/your/image.jpg'));
const response = await axios.post('http://localhost:5000/api/admin/products/add', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
console.log('Product upload response:', response.data);
} catch (error) {
console.error('Error during product upload test:', error.response?.data || error.message);
}
}If you encounter "File size should be less than 5MB" errors:
- Ensure the server is using the updated middleware with 20MB limit
- Check that the client form validation allows 20MB files
If images aren't uploading to Cloudinary:
- Verify Cloudinary environment variables are correctly set
- Check Cloudinary account has sufficient storage
- Ensure internet connectivity for API calls
If you encounter CORS errors:
- Ensure the server has proper CORS configuration
- Check that frontend and backend URLs match allowed origins
- express
- mongoose
- multer
- cloudinary
- cors
- dotenv
- react
- axios
- lucide-react
- tailwindcss
- shadcn/ui components
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a pull request
This project is licensed under the MIT License.