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.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);

    // The `path` property is where formidable stored the file

    // You can read the file and print it out.
    fs.readFile(files[firstFileName].path, function(err, content) {
      if (err != null) {
        return res.json({ message: err.message });
      res.json({ content: content.toString('utf8') });

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