File Uploads with Express

May 22, 2019

Formidable is the most popular file uploading library for Express. Here's how you can use Formidable to upload files:

const app = require('express')();
const formidable = require('formidable');
const fs = require('fs');'/upload', function(req, res) {
  const form = new formidable.IncomingForm();
  // Parse `req` and upload all associated files
  form.parse(req, function(err, fields, files) {
    if (err != null) {
      return res.status(400).json({ message: err.message });

    // The `files` object contains all files that were uploaded. Formidable
    // parses each file and uploads it to a temporary file for you.
    const [firstFileName] = Object.keys(files);

    res.json({ filename: firstFileName });

const server = await app.listen(3000);

The /upload route handler has 3 steps:

1) Create a new form using new formidable.IncomingForm(). The IncomingForm class is the primary entry point to Formidable.

2) Call form.parse() on an Express request. This tells Formidable to parse the request and save any files in the request to your server.

3) Handle the uploaded files. You can store the files locally, or upload the files to a service like Amazon S3.

More Express Tutorials