busboy vs connect-busboy vs express-fileupload vs form-data vs formidable vs koa-body vs multer
文件上传处理库
busboyconnect-busboyexpress-fileuploadform-dataformidablekoa-bodymulter类似的npm包:

文件上传处理库

这些库用于处理文件上传,提供了不同的功能和灵活性,以适应不同的应用场景和需求。它们可以帮助开发者轻松地接收和处理来自客户端的文件数据,支持多种文件格式和上传方式,适用于Node.js环境。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
busboy03,001124 kB39--
connect-busboy01564.57 kB1--
express-fileupload01,557119 kB251 年前MIT
form-data02,35483.9 kB1412 个月前MIT
formidable0-204 kB-1 年前MIT
koa-body095563.8 kB93 个月前MIT
multer012,08132.8 kB2912 个月前MIT

功能对比: busboy vs connect-busboy vs express-fileupload vs form-data vs formidable vs koa-body vs multer

文件解析方式

  • busboy:

    Busboy使用流式解析方式,能够逐块处理上传的数据,适合处理大文件,内存占用低。

  • connect-busboy:

    Connect-Busboy基于Busboy,提供中间件支持,能够在请求处理过程中流式解析文件。

  • express-fileupload:

    Express-Fileupload使用简单的请求体解析,适合小文件上传,易于使用。

  • form-data:

    Form-Data用于构建和发送表单数据,支持流式和缓冲区的方式,灵活性高。

  • formidable:

    Formidable支持流式和缓冲区解析,能够处理复杂的表单数据和多文件上传。

  • koa-body:

    Koa-Body支持流式和缓冲区解析,能够处理多种数据格式,适合Koa框架。

  • multer:

    Multer使用内存或磁盘存储方式,能够处理多文件上传,支持文件过滤和大小限制。

集成与兼容性

  • busboy:

    Busboy是一个独立的库,可以与任何Node.js框架兼容使用,灵活性高。

  • connect-busboy:

    Connect-Busboy专为Connect和Express设计,能够无缝集成,使用简单。

  • express-fileupload:

    Express-Fileupload是专为Express设计的中间件,易于集成,适合快速开发。

  • form-data:

    Form-Data可以在客户端和服务器之间使用,适合需要发送表单数据的场景。

  • formidable:

    Formidable可以与任何Node.js框架兼容,功能强大,适合复杂应用。

  • koa-body:

    Koa-Body专为Koa框架设计,能够轻松集成,支持多种数据格式。

  • multer:

    Multer是专为Express设计的中间件,能够与Express无缝集成,功能强大。

学习曲线

  • busboy:

    Busboy的API相对简单,但需要对流式处理有一定了解,适合有经验的开发者。

  • connect-busboy:

    Connect-Busboy易于使用,适合初学者,特别是已熟悉Express的开发者。

  • express-fileupload:

    Express-Fileupload非常简单,几乎没有学习曲线,适合快速上手。

  • form-data:

    Form-Data的使用相对简单,适合需要发送表单数据的开发者。

  • formidable:

    Formidable功能强大,学习曲线较陡,适合需要复杂文件上传处理的开发者。

  • koa-body:

    Koa-Body的使用较为简单,适合Koa框架的开发者,易于上手。

  • multer:

    Multer的API设计合理,学习曲线适中,适合需要处理文件上传的开发者。

性能

  • busboy:

    Busboy在处理大文件时性能优越,内存占用低,适合高并发场景。

  • connect-busboy:

    Connect-Busboy性能良好,适合中小型应用,能够高效处理文件上传。

  • express-fileupload:

    Express-Fileupload在处理小文件时性能良好,但不适合大文件上传。

  • form-data:

    Form-Data在构建表单数据时性能高效,适合快速发送数据。

  • formidable:

    Formidable性能强大,能够处理复杂表单和大文件上传,适合高负载应用。

  • koa-body:

    Koa-Body性能良好,适合Koa框架的应用,能够高效处理请求体。

  • multer:

    Multer在处理多文件上传时性能优越,支持多种存储方式,适合高并发场景。

扩展性

  • busboy:

    Busboy的设计允许开发者根据需要进行扩展,适合需要定制化的场景。

  • connect-busboy:

    Connect-Busboy可以与其他中间件结合使用,扩展性强。

  • express-fileupload:

    Express-Fileupload简单易用,但扩展性有限,适合小型项目。

  • form-data:

    Form-Data提供了灵活的API,适合需要自定义表单数据的开发者。

  • formidable:

    Formidable功能强大,支持多种扩展,适合复杂应用。

  • koa-body:

    Koa-Body易于扩展,适合Koa框架的开发者。

  • multer:

    Multer支持多种存储选项和文件过滤功能,扩展性强,适合复杂应用。

如何选择: busboy vs connect-busboy vs express-fileupload vs form-data vs formidable vs koa-body vs multer

  • busboy:

    选择Busboy,如果你需要一个轻量级的流式解析器,能够高效处理大型文件上传,并且希望对上传过程有更细粒度的控制。

  • connect-busboy:

    选择Connect-Busboy,如果你使用Connect或Express框架,并希望在中间件中轻松集成文件上传功能。

  • express-fileupload:

    选择Express-Fileupload,如果你需要一个简单易用的解决方案,能够快速实现文件上传功能,适合小型项目或快速原型开发。

  • form-data:

    选择Form-Data,如果你需要在客户端和服务器之间发送表单数据,尤其是需要支持文件上传的场景,它提供了灵活的API来构建表单数据。

  • formidable:

    选择Formidable,如果你需要一个功能强大的文件上传库,支持多种文件类型和复杂的表单数据解析,适合需要处理复杂上传场景的应用。

  • koa-body:

    选择Koa-Body,如果你使用Koa框架,想要一个简单的中间件来处理请求体,包括文件上传,支持多种数据格式。

  • multer:

    选择Multer,如果你需要与Express框架紧密集成的文件上传中间件,支持多种存储选项和文件过滤功能,适合需要处理多文件上传的应用。

busboy的README

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.