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 typically POST, PUT, or PATCH; for a create-style request we use POST.
  • enctype — how the form data should be encoded when submitted. It can be application/x-www-form-urlencoded, multipart/form-data, or text/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.

VS Code showing a basic Express app.js that serves index.html and listens on port 3000
A minimal Express server before Multer—serving the HTML page and static assets.

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.

VS Code showing index.html with a form for name, Developer and Designer checkboxes, and a commented-out file input
The starter form: text and checkboxes only; the file input is still commented out.

And the web page looks like this:

Browser at 127.0.0.1:3000 showing Hello World and an empty form with name, checkboxes, and Submit
The form in the browser before any data is entered.

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.' })
})
VS Code showing app.js with a POST /saveFormData route that logs req.body and returns JSON
The /saveFormData route before any body-parsing middleware.

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:

Browser at 127.0.0.1:3000/saveFormData showing JSON status success and Data successfully uploaded
The JSON response after submitting the form.

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:

VS Code showing app.js with multer, upload.none on the POST route, and express.urlencoded middleware
Multer with upload.none() plus express.urlencoded() for text-only forms.

Fill in the form and submit:

Browser form filled with Akshat Trivedi and both Developer and Designer checkboxes checked
Submitting text fields and checkboxes after urlencoded parsing is in place.

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

Browser at 127.0.0.1:3000/saveFormData showing the success JSON response again
The same success JSON after the body parsers are wired up.

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

VS Code terminal logging req.body with name Akshat Trivedi and skills developer and designer
req.body populated after express.urlencoded() parses the form.

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:

Browser form open with a Windows file picker selecting goku from Pictures
Choosing an image to upload through the file input.
Browser form with name Akshat, both skills checked, and goku.jpg selected
The form ready to submit with text fields and goku.jpg.

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:

VS Code showing a random hex-named file in public that cannot be displayed as text, with req.body logged in the terminal
Multer’s default dest naming: a random hash in public, plus the text fields in req.body.

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:

VS Code image preview of the renamed upload showing a stylized Goku illustration
After adding a .png extension, the uploaded file opens as the original image.

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 receives req, file, and a callback. The first callback argument is an error (null if none); the second is the destination path.
  • filename — how the file is named. Same parameters as destination. Here we build a unique name, then call the callback with null and 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-data is 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 with upload.single()
  • How to customize storage with diskStorage, generate unique names, and filter file types
  • How to read uploaded data from req.body and req.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.