Overview
File uploads are handled by themulter middleware, which:
- Stores files on disk in organized directories
- Validates file types (images only)
- Enforces file size limits (5MB maximum)
- Generates unique filenames to prevent conflicts
Upload configuration
The upload middleware is configured inmiddlewares/fileUpload.mjs:
middlewares/fileUpload.mjs
Supported file types
The API accepts the following image formats:- JPEG (
.jpg,.jpeg) - PNG (
.png) - GIF (
.gif) - BMP (
.bmp) - WebP (
.webp)
File size limits
Maximum file size: 5MB (5,242,880 bytes) Files exceeding this limit will be rejected with a size limit error.Storage structure
Files are automatically organized by route:Accessing uploaded files
Uploaded files are served statically and accessible via URL:app.mjs
Upload examples
Single file upload - Profile photo
Update a user’s profile photo: Route configuration:routes/authenticateRoutes.mjs
controllers/userController.mjs
Multiple file upload - Product images
Add a product with multiple images: Route configuration:routes/adminRoutes.mjs
controllers/productController.mjs
File object properties
When a file is successfully uploaded, thereq.file (single) or req.files (multiple) object contains:
Error handling
The API includes an upload error handler middleware:Best practices
Clean up old files
Implement a cleanup strategy to remove old or unused files to save storage space.
Troubleshooting
Upload directory permissions
Ensure the application has write permissions for the uploads directory:Path separator issues
The code handles Windows path separators:Missing files in response
If uploaded files aren’t accessible, verify the static file serving is configured:app.mjs
Next steps
Products API
Learn how to manage products with images
User API
Update user profiles with photos