Node.js › File Uploads & Media Processing

What is multipart/form-data, and why does a Node.js server need special middleware (like multer) to handle file uploads?

Beginner

Answer

multipart/form-data is the HTTP content type browsers use to submit forms containing file uploads, encoding each form field (including file contents) as a separate 'part' within the request body, separated by a boundary string -- Express's built-in body-parsing middleware (express.json/urlencoded) doesn't understand this format, so a dedicated middleware like multer is needed to parse the multipart body, extract uploaded files (saving them to disk or memory), and populate req.file/req.files with the parsed results.
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });

app.post('/upload', upload.single('avatar'), (req, res) => {
  console.log(req.file); // { filename, path, size, mimetype, ... }
  res.json({ success: true });
});
Real-world example A profile-picture upload endpoint uses multer configured to save uploaded files to a temporary directory, then processes the resulting req.file to move it to permanent storage after validating its size and type.

Common follow-ups: Why can't express.json() parse a multipart/form-data request the way it parses a JSON body?;What's the difference between multer's disk storage and memory storage engines?