busboy vs connect-busboy vs express-fileupload vs form-data vs formidable vs koa-body vs multer
Node.js File Upload Libraries
busboyconnect-busboyexpress-fileuploadform-dataformidablekoa-bodymulterSimilar Packages:

Node.js File Upload Libraries

These libraries facilitate file uploads in Node.js applications, providing various methods to handle multipart/form-data. They differ in terms of middleware integration, performance, and feature sets, catering to different use cases and frameworks such as Express and Koa. Understanding their unique functionalities helps developers choose the right tool for their specific needs.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
busboy02,999124 kB37--
connect-busboy01564.57 kB0--
express-fileupload01,561119 kB2410 months agoMIT
form-data02,34981.2 kB1385 months agoMIT
formidable0-204 kB-a year agoMIT
koa-body095260.7 kB104 months agoMIT
multer012,03931.5 kB2552 months agoMIT

Feature Comparison: busboy vs connect-busboy vs express-fileupload vs form-data vs formidable vs koa-body vs multer

Integration

  • busboy:

    Busboy is a low-level streaming parser that requires manual integration into your application, giving you full control over the file upload process but requiring more setup.

  • connect-busboy:

    Connect-Busboy integrates Busboy with Connect middleware, simplifying the process of handling file uploads in Connect-based applications.

  • express-fileupload:

    Express-Fileupload is designed specifically for Express, providing a plug-and-play solution for file uploads with minimal configuration needed.

  • form-data:

    Form-Data is primarily used for constructing multipart/form-data requests, making it suitable for client-side applications rather than server-side file handling.

  • formidable:

    Formidable can be used with any Node.js application, providing a comprehensive solution for file uploads and form parsing, but it requires more setup than simpler libraries.

  • koa-body:

    Koa-Body integrates seamlessly with Koa, allowing for easy handling of multipart/form-data in Koa applications, with a straightforward API.

  • multer:

    Multer is a middleware for Express that simplifies file uploads, providing built-in support for various storage options and configuration.

Performance

  • busboy:

    Busboy is highly efficient for large file uploads due to its streaming nature, which minimizes memory usage and allows for processing files in chunks.

  • connect-busboy:

    Performance is similar to Busboy, as it utilizes the same underlying library, but may introduce slight overhead due to middleware integration.

  • express-fileupload:

    Express-Fileupload is easy to use but may not be as performant as Busboy for very large files, as it buffers the entire file in memory before processing.

  • form-data:

    Form-Data is optimized for creating requests but is not designed for handling uploads on the server side, so performance considerations depend on the use case.

  • formidable:

    Formidable is efficient for file uploads but can consume more memory than Busboy, especially with large files, as it buffers uploads before processing.

  • koa-body:

    Koa-Body is designed for Koa applications and offers good performance, but like Express-Fileupload, it may buffer files in memory depending on configuration.

  • multer:

    Multer provides excellent performance for file uploads in Express applications, with options to store files in memory or on disk, allowing for flexible handling.

Ease of Use

  • busboy:

    Busboy requires more manual setup and configuration, making it less beginner-friendly but offering greater control for experienced developers.

  • connect-busboy:

    Connect-Busboy is straightforward to use within Connect applications, providing a simple API for file uploads without much boilerplate code.

  • express-fileupload:

    Express-Fileupload is very easy to use, with minimal setup required, making it ideal for beginners or quick prototypes.

  • form-data:

    Form-Data is easy to use for constructing requests but is not focused on server-side file handling, which may require additional libraries for uploads.

  • formidable:

    Formidable has a moderate learning curve, offering comprehensive functionality but requiring more understanding of its API and options.

  • koa-body:

    Koa-Body is user-friendly for Koa applications, providing a clean API for handling file uploads and form data with minimal configuration.

  • multer:

    Multer is designed to be easy to use with Express, allowing developers to quickly set up file uploads with clear configuration options.

File Handling Features

  • busboy:

    Busboy allows for streaming file uploads, enabling developers to process files as they are being uploaded, which is useful for large files.

  • connect-busboy:

    Connect-Busboy inherits Busboy's file handling capabilities, allowing for streaming uploads within Connect applications.

  • express-fileupload:

    Express-Fileupload provides simple file handling features but lacks advanced options for file processing compared to other libraries.

  • form-data:

    Form-Data focuses on creating multipart requests and does not handle file uploads on the server side, requiring additional handling for uploads.

  • formidable:

    Formidable offers robust file handling features, including file renaming, size limits, and error handling, making it suitable for complex applications.

  • koa-body:

    Koa-Body supports file uploads and provides a clean API for accessing uploaded files, making it easy to work with in Koa applications.

  • multer:

    Multer provides extensive file handling features, including storage options, file size limits, and file filtering, making it highly configurable for various use cases.

Community and Support

  • busboy:

    Busboy has a smaller community compared to some other libraries, but it is well-maintained and widely used in production applications.

  • connect-busboy:

    Connect-Busboy benefits from the Connect community, which provides support and resources for users.

  • express-fileupload:

    Express-Fileupload has a large user base and community support, making it easy to find help and resources online.

  • form-data:

    Form-Data is widely used for constructing requests and has good community support, but it is not focused on server-side uploads.

  • formidable:

    Formidable has been around for a while and has a solid community, providing ample resources and support for developers.

  • koa-body:

    Koa-Body is part of the Koa ecosystem, which has a growing community and support for Koa-related libraries.

  • multer:

    Multer has a large community and is widely adopted in Express applications, ensuring good support and documentation.

How to Choose: busboy vs connect-busboy vs express-fileupload vs form-data vs formidable vs koa-body vs multer

  • busboy:

    Choose Busboy for a lightweight, low-level streaming parser that is efficient for handling large file uploads without additional overhead. It is ideal for applications that require fine-grained control over file processing.

  • connect-busboy:

    Select Connect-Busboy if you are using Connect middleware and need a simple way to integrate Busboy with your existing Connect-based application. It offers a straightforward approach to handle file uploads seamlessly within Connect.

  • express-fileupload:

    Use Express-Fileupload for an easy-to-use solution that integrates directly with Express. It simplifies file upload handling with minimal setup, making it suitable for quick projects or prototypes.

  • form-data:

    Opt for Form-Data when you need to construct and send multipart/form-data requests from Node.js. It is particularly useful for client-side applications that require file uploads to a server.

  • formidable:

    Choose Formidable for a robust file upload solution that supports file uploads and form parsing. It is well-suited for applications that require detailed file handling and form data processing.

  • koa-body:

    Select Koa-Body if you are building applications with Koa and need a middleware that supports multipart/form-data. It provides a clean API for handling file uploads and form data in Koa applications.

  • multer:

    Use Multer when working with Express and need a powerful middleware for handling multipart/form-data. It offers extensive configuration options for file storage and limits, making it ideal for complex file upload scenarios.

README for busboy

Description

A node.js module for parsing incoming HTML form data.

Changes (breaking or otherwise) in v1.0.0 can be found here.

Requirements

Install

npm install busboy

Examples

  • Parsing (multipart) with default options:
const http = require('http');

const busboy = require('busboy');

http.createServer((req, res) => {
  if (req.method === 'POST') {
    console.log('POST request');
    const bb = busboy({ headers: req.headers });
    bb.on('file', (name, file, info) => {
      const { filename, encoding, mimeType } = info;
      console.log(
        `File [${name}]: filename: %j, encoding: %j, mimeType: %j`,
        filename,
        encoding,
        mimeType
      );
      file.on('data', (data) => {
        console.log(`File [${name}] got ${data.length} bytes`);
      }).on('close', () => {
        console.log(`File [${name}] done`);
      });
    });
    bb.on('field', (name, val, info) => {
      console.log(`Field [${name}]: value: %j`, val);
    });
    bb.on('close', () => {
      console.log('Done parsing form!');
      res.writeHead(303, { Connection: 'close', Location: '/' });
      res.end();
    });
    req.pipe(bb);
  } else if (req.method === 'GET') {
    res.writeHead(200, { Connection: 'close' });
    res.end(`
      <html>
        <head></head>
        <body>
          <form method="POST" enctype="multipart/form-data">
            <input type="file" name="filefield"><br />
            <input type="text" name="textfield"><br />
            <input type="submit">
          </form>
        </body>
      </html>
    `);
  }
}).listen(8000, () => {
  console.log('Listening for requests');
});

// Example output:
//
// Listening for requests
//   < ... form submitted ... >
// POST request
// File [filefield]: filename: "logo.jpg", encoding: "binary", mime: "image/jpeg"
// File [filefield] got 11912 bytes
// Field [textfield]: value: "testing! :-)"
// File [filefield] done
// Done parsing form!
  • Save all incoming files to disk:
const { randomFillSync } = require('crypto');
const fs = require('fs');
const http = require('http');
const os = require('os');
const path = require('path');

const busboy = require('busboy');

const random = (() => {
  const buf = Buffer.alloc(16);
  return () => randomFillSync(buf).toString('hex');
})();

http.createServer((req, res) => {
  if (req.method === 'POST') {
    const bb = busboy({ headers: req.headers });
    bb.on('file', (name, file, info) => {
      const saveTo = path.join(os.tmpdir(), `busboy-upload-${random()}`);
      file.pipe(fs.createWriteStream(saveTo));
    });
    bb.on('close', () => {
      res.writeHead(200, { 'Connection': 'close' });
      res.end(`That's all folks!`);
    });
    req.pipe(bb);
    return;
  }
  res.writeHead(404);
  res.end();
}).listen(8000, () => {
  console.log('Listening for requests');
});

API

Exports

busboy exports a single function:

( function )(< object >config) - Creates and returns a new Writable form parser stream.

  • Valid config properties:

    • headers - object - These are the HTTP headers of the incoming request, which are used by individual parsers.

    • highWaterMark - integer - highWaterMark to use for the parser stream. Default: node's stream.Writable default.

    • fileHwm - integer - highWaterMark to use for individual file streams. Default: node's stream.Readable default.

    • defCharset - string - Default character set to use when one isn't defined. Default: 'utf8'.

    • defParamCharset - string - For multipart forms, the default character set to use for values of part header parameters (e.g. filename) that are not extended parameters (that contain an explicit charset). Default: 'latin1'.

    • preservePath - boolean - If paths in filenames from file parts in a 'multipart/form-data' request shall be preserved. Default: false.

    • limits - object - Various limits on incoming data. Valid properties are:

      • fieldNameSize - integer - Max field name size (in bytes). Default: 100.

      • fieldSize - integer - Max field value size (in bytes). Default: 1048576 (1MB).

      • fields - integer - Max number of non-file fields. Default: Infinity.

      • fileSize - integer - For multipart forms, the max file size (in bytes). Default: Infinity.

      • files - integer - For multipart forms, the max number of file fields. Default: Infinity.

      • parts - integer - For multipart forms, the max number of parts (fields + files). Default: Infinity.

      • headerPairs - integer - For multipart forms, the max number of header key-value pairs to parse. Default: 2000 (same as node's http module).

This function can throw exceptions if there is something wrong with the values in config. For example, if the Content-Type in headers is missing entirely, is not a supported type, or is missing the boundary for 'multipart/form-data' requests.

(Special) Parser stream events

  • file(< string >name, < Readable >stream, < object >info) - Emitted for each new file found. name contains the form field name. stream is a Readable stream containing the file's data. No transformations/conversions (e.g. base64 to raw binary) are done on the file's data. info contains the following properties:

    • filename - string - If supplied, this contains the file's filename. WARNING: You should almost never use this value as-is (especially if you are using preservePath: true in your config) as it could contain malicious input. You are better off generating your own (safe) filenames, or at the very least using a hash of the filename.

    • encoding - string - The file's 'Content-Transfer-Encoding' value.

    • mimeType - string - The file's 'Content-Type' value.

    Note: If you listen for this event, you should always consume the stream whether you care about its contents or not (you can simply do stream.resume(); if you want to discard/skip the contents), otherwise the 'finish'/'close' event will never fire on the busboy parser stream. However, if you aren't accepting files, you can either simply not listen for the 'file' event at all or set limits.files to 0, and any/all files will be automatically skipped (these skipped files will still count towards any configured limits.files and limits.parts limits though).

    Note: If a configured limits.fileSize limit was reached for a file, stream will both have a boolean property truncated set to true (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens.

  • field(< string >name, < string >value, < object >info) - Emitted for each new non-file field found. name contains the form field name. value contains the string value of the field. info contains the following properties:

    • nameTruncated - boolean - Whether name was truncated or not (due to a configured limits.fieldNameSize limit)

    • valueTruncated - boolean - Whether value was truncated or not (due to a configured limits.fieldSize limit)

    • encoding - string - The field's 'Content-Transfer-Encoding' value.

    • mimeType - string - The field's 'Content-Type' value.

  • partsLimit() - Emitted when the configured limits.parts limit has been reached. No more 'file' or 'field' events will be emitted.

  • filesLimit() - Emitted when the configured limits.files limit has been reached. No more 'file' events will be emitted.

  • fieldsLimit() - Emitted when the configured limits.fields limit has been reached. No more 'field' events will be emitted.