Sitemap

Multer in Node.js: It’s more than File Uploads

3 min readSep 24, 2025

--

When I first started working with file uploads in Node.js, it kind of felt like those boring “just do it once and forget it in the config folder” tasks. But once you really get into it, you tend to realize that Multer is not just a helper library; rather, it’s more of a gateway to understand how Node.js handles form data, streams, and file systems.

And if you are building anything — be it an e-commerce app that handles the product or profile images, or a CRM where clients upload their documents. You will eventually end up wrestling with file uploads.

What is Multer, really?

At its heart, Multer is middleware. Think of it as the traffic police for multipart/form-data.
When a user uploads an image, Node.js doesn’t understand it directly. The request body is a stream of binary data. Multer steps in, parses it, validates it, and saves it where you want.

Without it, you’d have to deal with raw streams, buffers, and lots of headaches.

Storage Engines — The Real Deal

The first thing to understand about Multer is storage.
You get two main options:

  1. DiskStorage — Saves files directly to your server (like in your /uploads folder).
  2. MemoryStorage — Keeps files in memory as Buffer objects (useful if you want to process them before saving, like resizing an image).

Here’s the simple trick: if your app needs uploaded files just for storage (e.g. profile pics, receipts), go for DiskStorage. But if you’re integrating with S3, Cloudinary, or even doing compression, then MemoryStorage is a better bet.

Setting Up Multer in Node.js

const multer = require("multer");
const path = require("path");
const fs = require("fs");
// Ensure uploads folder exists on your project dir
const uploadPath = path.join(__dirname, "..", "uploads");
if (!fs.existsSync(uploadPath)) {
fs.mkdirSync(uploadPath);
}
const storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, uploadPath),
filename: (req, file, cb) =>
cb(null, `${Date.now()}-${file.originalname}`),
}); //could be your custom
// good practice to name it with date&time to make it unique
const upload = multer({ storage });
module.exports = upload;

File Filters — Saying “No” at the Gate

One of the coolest (and underrated) features of Multer is the fileFilter function.
Imagine you’re running an Indian job portal, and candidates start uploading random PDFs, ZIP files, or even malware disguised as images. Scary, right?

Get Vishesh Verma’s stories in your inbox

Join Medium for free to get updates from this writer.

With fileFilter, you’re basically setting up a bouncer at the club’ s entry gate:

const fileFilter = (req, file, cb) => {
const allowedTypes = /jpeg|jpg|png|webp|gif/;
const extname = allowedTypes.test(
path.extname(file.originalname).toLowerCase()
);
const mimetype = allowedTypes.test(file.mimetype);
if (extname && mimetype) {
cb(null, true);
} else {
cb(new Error("Only image files are allowed!"));
}
};

Limits — Because Users Can Be Crazy

Trust me, you don’t want someone uploading a 5GB video file just because you forgot to set limits. Multer allows you to define fileSize, fields, and even the number of parts in a request.

const upload = multer({
storage,
fileFilter,
limits: { fileSize: 50 * 1024 * 1024 }, // 50MB size we have limited
});

It’s like saying: “Boss, max 50MB per file. Don’t try to upload Avengers here.”

Conclusion

Working with Multer is not just about learning file uploads. It teaches you:

  • How Node.js deals with streams and buffers
  • How validation and filtering work in middleware
  • How to make storage decisions (local vs cloud)

So, the next time someone says, “Oh, it’s justa file upload,” you can smile and say:

“Yes, and Multer is the one quietly making it possible.”

Press enter or click to view image in full size
Uploading files using Multer in Node.js

--

--

Vishesh Verma
Vishesh Verma

Written by Vishesh Verma

A beginner's journey, to be honest. A little bit of many, master of few. Sharing my findings, techniques, ideas & feedback.