I've recently had my (extremely novice) Node.js hat on and ran into a situation where I needed to deserialize a multipart/form-data type form submission on the server-side. Having been used to the body-parser library automatically transforming post data into JSON (https://github.com/expressjs/body-parser), I was surprised to find out that this library would not fly for a multipart form.

Enter multer, stage left. Multer is an node.js middleware designed for handling multipart/form-data type submissions. Multer will not be active for any other type of form. 

So say we have the below form:

<form action="/handle-submission" method="POST" encType="multipart/form-data">
    <fieldset id="documents">
                <span class="fileInputLabel">First document:</span>

                <input type="file" name="first" accept="application/pdf" required />
                <span class="fileInputLabel">Additional document <em>(optional)</em>:</span>
                <input type="file" name="second" accept="application/pdf" />

Which posts off to the /handle-submission endpoint of our Express server. We can simply pass in the Multer middleware instance to the router declaration (or alternatively we can register the Multer middleware more generally in our app.js file):

'use strict';

const express = require('express');
const router = express.Router();
const upload = multer();

function handleSubmission() {
    let files = req.files;

    // req.body will contain any other text fields from the request
    let body = req.body;

    // Do something with the files here.


// The upload.fields() call allows us to specify the expected files. The name attribute on each item directly corresponds to the HTML name attribute on each input[type="file"]
router.post('/handle-submission', upload.fields([{ name:"first", maxCount: 1 }, { name:"second", maxCount: 1 }]), handleSubmission);

module.exports = router;

Easy as pie!