Choose between Formidable, Busboy, Multer and Multiparty for processing file uploads

栏目: IT技术 · 发布时间: 5年前

内容简介:There are a few npm packages out there for reading and processing aBefore diving in, you want to decide:

There are a few npm packages out there for reading and processing a multipart/form-data request on a Node.js server. Each has their own design philosophy. Some are meant to be used with Express.js, some as standalone. Some store intermediate files in hard disk or in memory. Digging through each of them and choosing which one is right for you can take time. Here's a guide for deciding which library suits you best.

Before diving in, you want to decide:

  1. Do you need Express.js or not?
  2. Is saving intermediate files ok, or do you want to stream the files?
  3. If saving intermediate files is alright, would you prefer them in memory or on hard disk?

Breakdown of most popular packages

Formidable has been around the longest, having released its 1.0 version in 2011. It also has the most weekly downloads on npm. It's not Express.js specific, and it saves files in temporary directory on hard disk.

Busboy is an event-based streaming parser that's not tied to Express.js. Instead of storing intermediate files, it provides a stream to the incoming file. It's been around since 2013. The core multipart/form-data implementation has been extracted to a separate dicer module.

Multer is an Express.js middleware that's been around since 2015. It saves intermediate files either in memory or on hard disk and populates req.files object for consuming the files. You can have fine-grained control over which fields allow files and limit the number of uploaded files. Internally, Multer uses Busboy.

Multiparty is a fork of formidable from around 2013. It has the same functionality as Multiparty, but also allows streaming the files. However, in their documentation, Multiparty recommends using Busboy for a faster alternative.

Formidable Busboy Multer Multiparty
npm weekly downloads 2.1M 1.2M 0.6M 0.3M
Year 2010 2013 2015 2013
Express.js middleware
Streams
Intermediate files on disk
Intermediate files in memory
Breakdown of popular file upload processing libraries.

Use Formidable or Multer for proof of concepts and low volume

There are cases where you don't mind storing intermediate files. It could be that you're writing a prototype, or that you're going to have at most few admin users handling the file upload duties. Either way, you don't mind the server potentially getting busted by memory running out or a hard disk filling up. This is perfectly ok for some cases.

Think if you need Express and whether you'd like the files in memory or on hard disk.

Without Express: use Formidable

If you want to process file uploads outside of Express.js, you've got two options: Formidable and Multiparty. Multiparty is a fork of Formidable with added streams. But, Multiparty recommends using Busboy. Formidable has 7x more users; thus, in the case of something breaks, you're most likely get more help from the Formidable community.

Keep in mind that in this case, you don't have the option of storing files in memory, but only saving to temp files on disk.

Express.js and files in memory: use Multer

You can use the standalone libraries together with Express.js. So, you've got three options: Formidable, Multer and Multiparty.

If you want to store files in memory, it's an easy pick. Only Multer supports it.

Express.js and temporary files on disk: use Multer or Formidable

If you want to save files in a temporary directory on disk, you've got two options Multer and Formidable. Both are good choices with solid user-base. If you want more fine-grained control over what input fields allow files and limit the number of files, go with Multer.

store files in memory store files on disk
Express.js Multer Multer (for fine-grained control)
Formidable
without Express.js - Formidable
Choosing the right library with intermediate files.

Use Busboy for high-volume production-grade solution

When you've got a high-volume situation and want the most reliability from your server, the best option is to not store intermediate files on the Node.js server at all. Instead, you want to push the files to a separate file server as soon as you receive them. The file server can be a cloud storage service such as AWS S3 or a database that supports BLOBs.

To make this happen, streams are the optimal way of handling the incoming file data. The alternatives are Busboy and Multiparty. But, since Multiparty recommends using Busboy, we have an easy pick: use Busboy.

Visual guide

Here's a visual guide to looking at the choices.

Choose between Formidable, Busboy, Multer and Multiparty for processing file uploads

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

风吹江南之互联网金融

风吹江南之互联网金融

陈宇(江南愤青) / 东方出版社 / 2014-6-1 / 55元

随着中国互联网金融浪潮高涨,P2P、众筹、余额宝、微信支付等新生事物层出不穷,加之大数据等时髦概念助阵,简直是乱花渐欲迷人眼,令媒体兴奋,公众狂热。那么,互联网金融真的能“颠覆”传统金融吗?当互联网思维对撞传统金融观念,是互联网金融的一统天下,还是传统金融业的自我革新?究竟是谁动了金融业的奶酪? 本书作者早期试水创立具有互联网金融雏形的网站,后来成为互联网金融的资深投资人,基于其多年在该领域......一起来看看 《风吹江南之互联网金融》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换