Why express.json() Doesn't Handle Files
File uploads use a different request encoding — multipart/form-data — not JSON. express.json() only parses JSON bodies, so a separate middleware like multer is needed to parse file uploads.
Installing and Setting Up multer
npm install multerHandling a Single File Upload
Console Output
Click “Run” to see the console output here.
Handling Multiple Files
Console Output
Click “Run” to see the console output here.
Common multer Options
| Option | Purpose |
|---|---|
| dest | Local folder to save uploaded files |
| limits: { fileSize } | Reject files above a size limit |
| fileFilter | Reject files based on type (e.g. only images) |
| storage: multer.memoryStorage() | Keep files in memory instead of disk, e.g. to upload directly to cloud storage |
Always Set a File Size Limit
Without an explicit limits.fileSize, a malicious or accidental upload of an enormous file can exhaust server disk space or memory — always cap the size, and validate the file type server-side, not just on the frontend.
Restricting File Size and Type
Console Output
Click “Run” to see the console output here.
Best Practice
For production apps, upload files directly to object storage (like S3 or Cloudinary) instead of the local disk — a local uploads folder doesn't survive redeploys or scale across multiple server instances.