DevAcademy
LearnNode.jsFile Uploads
IntermediateNode.js

File Uploads

Handle file uploads in Express with multer, a middleware for parsing multipart form data.

Reading Time

12 min

Lesson

Lesson 26 of 34

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 multer

Handling a Single File Upload

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Handling Multiple Files

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Common multer Options

OptionPurpose
destLocal folder to save uploaded files
limits: { fileSize }Reject files above a size limit
fileFilterReject 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

Try it yourself — edit and run

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.

Interview Questions

Quick Quiz

1. Why can't express.json() parse file uploads?

2. Why is setting a file size limit important?

3. Why prefer uploading files to object storage (like S3) over local disk in production?