In this article you’ll learn how to use Multer to accept and process multipart form data. multipart/form-data is one of the values for the form enctype attribute—multipart means the payload is split and sent in multiple parts because it includes a file upload. We’ll create a route that listens for POST requests at /saveFormData, using Multer, middleware for handling multipart/form-data (primarily for file uploads).
Prerequisites
There are a few things you should know about forms before we start. The form tag has many attributes, but three matter most here—you should have a clear idea of what they do:
<form action="/saveFormData" method="post" enctype="multipart/form-data">
...
</form>
action— where the form data should be sent.method— the HTTP method for the request. When sending data it is typicallyPOST,PUT, orPATCH; for a create-style request we usePOST.enctype— how the form data should be encoded when submitted. It can beapplication/x-www-form-urlencoded,multipart/form-data, ortext/plain.
I’ve never needed text/plain. If you omit enctype, it defaults to application/x-www-form-urlencoded, and you cannot send file data with that encoding. If the form has a file input, enctype must be multipart/form-data.
Handling form data
Let’s start simple: handle a normal form with text and checkbox fields first. At this point app.js is a minimal Express server that serves an HTML page.

The index.html page contains a form with one text input and two checkboxes (the file input is commented out for now). We’ll look at sending that data to the backend and reading it there.

And the web page looks like this:

Sending form data with HTML attributes
To handle incoming POST requests, create a POST route:
app.post('/saveFormData', (req, res) => {
console.log(req.body)
res.status(200).json({ status: 'success', message: 'Data successfully uploaded.' })
})

We’ve created a /saveFormData route that logs the request body and returns JSON. In index.html, point the form at that route:
<form method="post" action="/saveFormData" class="form">
...
</form>
Submit the form and you should see the returned JSON from the server:

So our form data is going to the place we wanted, but we can’t seem to access it in req.body—the console logs undefined because the body is empty. That’s because we need to parse the form data before we can read it, and that’s why we need Multer. Install it:
npm i multer --save
Import it in app.js and create an upload instance:
const multer = require('multer')
const upload = multer()
Calling multer() returns an object we store as upload—similar to importing Express and calling it to get app.
Route handlers can chain middleware. Before the handler function, add upload.none():
app.post('/saveFormData', upload.none(), (req, res) => {
console.log(req.body)
res.status(200).json({ status: 'success', message: 'Data successfully uploaded.' })
})
.none() is for when you aren’t uploading files but still want Multer to handle the text fields.
Submit again and you’ll get the JSON response, but req.body may still be empty. The default enctype is application/x-www-form-urlencoded, so the data is URL-encoded and needs a different parser. Express provides that middleware—add:
app.use(express.urlencoded())
After everything, app.js looks something like this:

Fill in the form and submit:

When I hit submit, I get the following response from the server:

And my console looks like this, meaning the request body is getting populated with form data:

Now we can start with uploading a file.
Uploading a file
To send a file, set enctype to multipart/form-data. In index.html:
<form
action="/saveFormData"
method="post"
class="form"
enctype="multipart/form-data"
>
<input type="text" name="name" placeholder="Enter your name" />
<!-- checkboxes and other fields -->
<input type="file" name="profilePic" id="profilePic" />
</form>
Specify a name on every input—that becomes the property name on the request body (and for files, the field name Multer looks up).
You can remove app.use(express.urlencoded()) for this flow; with multipart/form-data the payload isn’t URL-encoded the same way.
Update app.js:
const upload = multer({ dest: path.join(__dirname, 'public') })
app.post('/saveFormData', upload.single('profilePic'), (req, res) => {
console.log(req.body)
res.status(200).json({ status: 'success', message: 'Data successfully uploaded.' })
})
We pass Multer an options object with dest: the path where files should be saved (here, a public folder next to the app).
In the route, upload.none() becomes upload.single('profilePic'). That accepts a single file whose field name is profilePic—matching <input type="file" name="profilePic" id="profilePic" />.
I’m uploading an image file:


When I hit submit, I again see the JSON response on the client side, but in the editor a file with a weird name appears inside public:

I cannot open it as text, but if I rename it with the extension .png I see that this is the same image I uploaded from the front end:

So the file did get uploaded—but we don’t just want to upload it. We want a unique, identifiable name, and we cannot allow any file type. Next: custom storage and a file filter.
Customizing file storage and naming
Right now Multer saves files with random names. In most apps you want control over:
- File name
- File format / extension
- Storage location
Multer’s storage engine covers that. Instead of the simple dest option, use diskStorage:
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.join(__dirname, 'public'))
},
filename: function (req, file, cb) {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9)
cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname))
},
})
const upload = multer({ storage: storage })
What’s going on:
destination— where the file is stored. It receivesreq,file, and a callback. The first callback argument is an error (nullif none); the second is the destination path.filename— how the file is named. Same parameters asdestination. Here we build a unique name, then call the callback withnulland that name.file.originalname— the original client filename.path.extname()— extracts the extension.
Uploaded files then look more like:
profilePic-1683029381234-123456789.png
Much better than an opaque random string with no extension.
Filtering file types
Never blindly accept every upload. That invites security risk, malicious files, and storage abuse. Restrict uploads to images:
const fileFilter = (req, file, cb) => {
if (file.mimetype.startsWith('image/')) {
cb(null, true)
}
else {
cb(new Error('Only image files are allowed!'), false)
}
}
const upload = multer({
storage: storage,
fileFilter: fileFilter,
})
Only image MIME types pass. Anything else makes the server reject the upload with an error.
Final route with file upload
app.post('/saveFormData', upload.single('profilePic'), (req, res) => {
console.log(req.body)
console.log(req.file)
res.status(200).json({ status: 'success', message: 'Data successfully uploaded.' })
})
Understanding req.file and req.body
After a successful upload:
req.body holds the text fields, for example:
{
"name": "Akshat",
"skills": ["developer", "designer"]
}
req.file holds file metadata:
{
"fieldname": "profilePic",
"originalname": "image.png",
"encoding": "7bit",
"mimetype": "image/png",
"destination": "public/",
"filename": "profilePic-1683029381234-123456789.png",
"path": "public/profilePic-1683029381234-123456789.png",
"size": 34567
}
Summary
In this article you learned:
- What
multipart/form-datais and why file uploads need it - How to use Multer as Express middleware
- How to handle text-only forms with
upload.none()and a single file withupload.single() - How to customize storage with
diskStorage, generate unique names, and filter file types - How to read uploaded data from
req.bodyandreq.file
Final thoughts
Multer is simple on the surface and powerful when used carefully. In production you might go further by:
- Uploading to cloud storage (AWS S3, Cloudinary)
- Enforcing file size limits
- Scanning uploads for malware
- Storing file metadata in a database
What you’ve covered here is the foundation—and where many developers get stuck at first. Once this is comfortable, file uploads become routine.
Companion code: github.com/akshat-triv/medium_articles (see the Multipart Form Data folder).
Thanks for reading—see you next time.



