- Document the 404 error for uploaded images - Explain that uploads directory needs to persist in production - Provide solutions: persistent directory or cloud storage - Add debug endpoint suggestion to check upload directory
1.9 KiB
1.9 KiB
Production Deployment Notes
File Uploads Issue
Problem
Images are returning 404 errors in production because the uploads directory is not persisting or is in the wrong location.
Current Configuration
- Backend serves uploads from:
/uploadsroute - Files are stored in:
UPLOAD_DIRenvironment variable (default:./uploads) - File URLs in database:
/uploads/photo-{timestamp}-{random}.jpeg
Solution Options
Option 1: Ensure Persistent Upload Directory (Recommended)
Make sure the uploads directory on the production server persists across deployments:
# On production server
cd /path/to/server
mkdir -p uploads
chmod 755 uploads
# Set in .env
UPLOAD_DIR=/absolute/path/to/uploads
Option 2: Use Cloud Storage (Better for Production)
Instead of local file storage, use cloud storage like AWS S3, Cloudinary, or similar:
- Update
server/src/middleware/upload.tsto use cloud storage - Update photo URLs to point to cloud storage
- No need for local uploads directory
Checking Upload Directory
Add this to your server to debug:
// In server/src/index.ts
app.get('/debug/uploads', (req, res) => {
const uploadDir = process.env.UPLOAD_DIR || path.join(process.cwd(), 'uploads');
const fs = require('fs');
res.json({
uploadDir,
exists: fs.existsSync(uploadDir),
files: fs.existsSync(uploadDir) ? fs.readdirSync(uploadDir) : [],
cwd: process.cwd(),
});
});
Current Status
- ✅ Frontend correctly requests:
https://litter-api.psbailey.uk/uploads/photo-xxx.jpeg - ✅ Backend serves uploads from
/uploadsroute - ❌ Files don't exist in the uploads directory on production server
Action Required
- Check if uploads directory exists on production server
- Verify UPLOAD_DIR environment variable is set correctly
- Ensure uploads directory has correct permissions
- Consider implementing cloud storage for production