Handling File Uploads in Express with Multer

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
Search for a command to run...

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
No comments yet. Be the first to comment.
Every day, millions of users upload photos, videos, stories, and reels to Instagram. From a user's perspective, the process appears simple: select media, apply filters, add a caption, and tap "Post."
Building Offline-First Messaging Apps: How Messages Work Without Internet Modern messaging applications have transformed the way people communicate. Whether it's chatting with friends, collaborating w
In this article we'll explore about the Expo Router and React Navigation and answer which one to use in 2026. If you build mobile apps using React Native, one thing becomes obvious very quickly: Navig

Modern mobile apps are no longer just a collection of screens connected together. Apps like Instagram, WhatsApp, Uber, and Netflix operate at massive scale with millions of users, real time systems, o
In this article we'll be exploring react.js and the things of react.js that makes it popular and stand out among other libraries ( no fight over library vs framework ). We'll go through: What problem

Shkaai
68 posts
Uploading files (like images, PDFs, etc.) is a common requirement in web applications. However, handling file uploads in Express is not straightforward without additional tools. This is where middleware like Multer becomes useful.
Let’s understand everything step by step.
When a client uploads a file, the request is sent as multipart/form-data, not JSON.
Express by default can handle:
JSON data (express.json())
URL-encoded data
But it cannot handle multipart/form-data directly.
This creates two problems:
You cannot access file data using req.body
The server cannot process or store uploaded files properly
To solve this, we use middleware that can:
Parse incoming file data
Store files on the server
Make file info available in req.file or req.files
Multer is a middleware for Express used to handle file uploads.
It helps you:
Parse multipart/form-data
Store files locally or in memory
Access uploaded file details easily
npm install multer
const multer = require('multer');
//or
import multer from "multer"
First, you need to configure Multer.
import multer from "multer"
import express from "express"
const app = express();
// store files in "uploads" folder
const upload = multer({ dest: 'uploads/' });
app.post('/upload', upload.single('file'), (req, res) => {
res.send('File uploaded successfully');
});
app.listen(3000);
'file' is the name of the input field in your formconsole.log(req.file);
Multer stores details like:
filename
path
size
mimetype
To upload multiple files, use .array().
app.post('/upload-multiple', upload.array('files', 3), (req, res) => {
res.send('Multiple files uploaded');
});
'files' → input field name
3 → maximum number of files
console.log(req.files);
You will get an array of file objects.
Instead of using default storage, you can customize how files are stored.
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/');
},
filename: function (req, file, cb) {
const uniqueName = Date.now() + '-' + file.originalname;
cb(null, uniqueName);
}
});
const upload = multer({ storage: storage });
destination → where file is stored
filename → how file is named
This helps avoid filename conflicts and organize files better.
After uploading files, you often want to access them in the browser.
Use Express static middleware:
app.use('/uploads', express.static('uploads'));
Now you can access files like:
http://your.domain/uploads/filename.jpg
This makes uploaded files publicly accessible.
File uploads require middleware because Express cannot handle multipart/form-data by default
Multer is used to process and store uploaded files
You can handle single and multiple file uploads easily
Storage configuration helps control file naming and location
Uploaded files can be served using static middleware
Handling file uploads in Express becomes simple with Multer. It acts as a bridge between incoming file data and your server by parsing multipart requests and storing files efficiently. By understanding how to upload single and multiple files, configure storage, and serve uploaded content, you can build features like profile image uploads, document submissions, and media sharing systems. Multer is an essential tool for creating real-world applications that deal with user-generated content.