body-parser vs express vs formidable vs multer
Node.js Middleware and File Upload Libraries Comparison
1 Year
body-parserexpressformidablemulterSimilar Packages:
What's Node.js Middleware and File Upload Libraries?

These packages are essential tools in Node.js web development, each serving unique roles in handling HTTP requests, parsing incoming request bodies, and managing file uploads. They streamline the development process by providing robust solutions for common tasks such as parsing JSON and URL-encoded data, handling multipart form data, and facilitating file uploads. Understanding their functionalities helps developers choose the right tool for their specific needs, enhancing application efficiency and maintainability.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
body-parser38,269,6805,46962.6 kB355 months agoMIT
express36,654,07866,336221 kB1762 months agoMIT
formidable11,301,3977,099203 kB514 months agoMIT
multer6,466,98111,70527.6 kB269-MIT
Feature Comparison: body-parser vs express vs formidable vs multer

Purpose

  • body-parser:

    body-parser is a middleware that parses incoming request bodies in a middleware before your handlers, making the data available under the req.body property. It supports JSON and URL-encoded formats, making it essential for APIs that receive data in these formats.

  • express:

    Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It simplifies routing, middleware integration, and request handling, serving as the backbone of many Node.js applications.

  • formidable:

    formidable is a Node.js module for parsing form data, especially file uploads. It is designed to handle multipart/form-data, which is used for uploading files, making it a go-to choice for applications that require file handling capabilities.

  • multer:

    multer is a middleware for handling multipart/form-data, primarily used for uploading files. It provides a simple API for configuring file storage and limits, making it ideal for applications that need to manage file uploads efficiently.

File Upload Handling

  • body-parser:

    body-parser does not handle file uploads; it focuses on parsing request bodies for JSON and URL-encoded data. It is not suitable for applications that require file upload capabilities.

  • express:

    Express does not handle file uploads directly but can be integrated with middleware like multer or formidable to manage file uploads effectively. It provides the routing and middleware framework to facilitate these integrations.

  • formidable:

    formidable excels at handling file uploads and parsing multipart form data. It provides a straightforward API for managing file uploads, making it suitable for applications that require robust file handling.

  • multer:

    multer is specifically designed for handling file uploads in Node.js applications. It provides a simple and efficient way to manage multipart/form-data, making it easy to integrate with express for file upload functionalities.

Integration

  • body-parser:

    body-parser can be easily integrated into express applications as middleware, allowing for seamless parsing of request bodies before they reach your route handlers.

  • express:

    Express serves as the core framework for building web applications, allowing for easy integration with various middleware, including body-parser, multer, and formidable, to enhance functionality.

  • formidable:

    formidable can be used as middleware in express applications, allowing for easy handling of file uploads and form data parsing. It integrates well with express routing.

  • multer:

    multer is designed to work seamlessly with express, providing middleware for handling multipart/form-data. It can be easily configured to manage file uploads in express routes.

Ease of Use

  • body-parser:

    body-parser is straightforward to use, requiring minimal configuration to start parsing request bodies. Its simplicity makes it a popular choice for developers needing quick and efficient body parsing.

  • express:

    Express is known for its simplicity and ease of use, allowing developers to set up a server and define routes with minimal effort. Its extensive documentation and community support make it accessible for developers of all skill levels.

  • formidable:

    formidable has a slightly steeper learning curve due to its focus on file uploads, but it provides a powerful API for managing complex file handling scenarios. Once learned, it offers great flexibility.

  • multer:

    multer is easy to use and configure, especially for developers familiar with express. It provides clear documentation and examples, making it accessible for those needing to implement file uploads quickly.

Performance

  • body-parser:

    body-parser is optimized for performance when parsing JSON and URL-encoded data, ensuring that request bodies are processed efficiently without significant overhead.

  • express:

    Express is designed for performance and scalability, allowing developers to build high-performance web applications. Its middleware architecture enables efficient request handling and routing.

  • formidable:

    formidable is efficient in handling file uploads, but performance can vary based on file size and server configuration. It is generally reliable for most use cases involving file uploads.

  • multer:

    multer is optimized for handling file uploads and can be configured for performance based on storage options and file size limits. It is designed to minimize overhead during file processing.

How to Choose: body-parser vs express vs formidable vs multer
  • body-parser:

    Choose body-parser if you need a simple and efficient way to parse incoming request bodies in a middleware format, especially for JSON and URL-encoded data. It's particularly useful for APIs that require data to be sent in these formats.

  • express:

    Choose express if you need a full-fledged web application framework that provides a robust set of features for web and mobile applications. It acts as a foundation for building server-side applications, allowing for easy routing, middleware integration, and HTTP request handling.

  • formidable:

    Choose formidable if you need a powerful solution for handling file uploads in Node.js applications. It provides a simple API for parsing form data, especially for multipart/form-data, and is well-suited for applications that require extensive file handling capabilities.

  • multer:

    Choose multer if you need a middleware specifically designed for handling multipart/form-data, primarily for file uploads. It is easy to integrate with express applications and supports various storage options, making it ideal for applications that require file uploads.

README for body-parser

body-parser

NPM Version NPM Downloads Build Status Test Coverage OpenSSF Scorecard Badge

Node.js body parsing middleware.

Parse incoming request bodies in a middleware before your handlers, available under the req.body property.

Note As req.body's shape is based on user-controlled input, all properties and values in this object are untrusted and should be validated before trusting. For example, req.body.foo.toString() may fail in multiple ways, for example the foo property may not be there or may not be a string, and toString may not be a function and instead a string or other user input.

Learn about the anatomy of an HTTP transaction in Node.js.

This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules:

This module provides the following parsers:

Other body parsers you might be interested in:

Installation

$ npm install body-parser

API

var bodyParser = require('body-parser')

The bodyParser object exposes various factories to create middlewares. All middlewares will populate the req.body property with the parsed body when the Content-Type request header matches the type option, or an empty object ({}) if there was no body to parse, the Content-Type was not matched, or an error occurred.

The various errors returned by this module are described in the errors section.

bodyParser.json([options])

Returns middleware that only parses json and only looks at requests where the Content-Type header matches the type option. This parser accepts any Unicode encoding of the body and supports automatic inflation of gzip and deflate encodings.

A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body).

Options

The json function takes an optional options object that may contain any of the following keys:

inflate

When set to true, then deflated (compressed) bodies will be inflated; when false, deflated bodies are rejected. Defaults to true.

limit

Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. Defaults to '100kb'.

reviver

The reviver option is passed directly to JSON.parse as the second argument. You can find more information on this argument in the MDN documentation about JSON.parse.

strict

When set to true, will only accept arrays and objects; when false will accept anything JSON.parse accepts. Defaults to true.

type

The type option is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function, type option is passed directly to the type-is library and this can be an extension name (like json), a mime type (like application/json), or a mime type with a wildcard (like */* or */json). If a function, the type option is called as fn(req) and the request is parsed if it returns a truthy value. Defaults to application/json.

verify

The verify option, if supplied, is called as verify(req, res, buf, encoding), where buf is a Buffer of the raw request body and encoding is the encoding of the request. The parsing can be aborted by throwing an error.

bodyParser.raw([options])

Returns middleware that parses all bodies as a Buffer and only looks at requests where the Content-Type header matches the type option. This parser supports automatic inflation of gzip and deflate encodings.

A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body). This will be a Buffer object of the body.

Options

The raw function takes an optional options object that may contain any of the following keys:

inflate

When set to true, then deflated (compressed) bodies will be inflated; when false, deflated bodies are rejected. Defaults to true.

limit

Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. Defaults to '100kb'.

type

The type option is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function, type option is passed directly to the type-is library and this can be an extension name (like bin), a mime type (like application/octet-stream), or a mime type with a wildcard (like */* or application/*). If a function, the type option is called as fn(req) and the request is parsed if it returns a truthy value. Defaults to application/octet-stream.

verify

The verify option, if supplied, is called as verify(req, res, buf, encoding), where buf is a Buffer of the raw request body and encoding is the encoding of the request. The parsing can be aborted by throwing an error.

bodyParser.text([options])

Returns middleware that parses all bodies as a string and only looks at requests where the Content-Type header matches the type option. This parser supports automatic inflation of gzip and deflate encodings.

A new body string containing the parsed data is populated on the request object after the middleware (i.e. req.body). This will be a string of the body.

Options

The text function takes an optional options object that may contain any of the following keys:

defaultCharset

Specify the default character set for the text content if the charset is not specified in the Content-Type header of the request. Defaults to utf-8.

inflate

When set to true, then deflated (compressed) bodies will be inflated; when false, deflated bodies are rejected. Defaults to true.

limit

Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. Defaults to '100kb'.

type

The type option is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function, type option is passed directly to the type-is library and this can be an extension name (like txt), a mime type (like text/plain), or a mime type with a wildcard (like */* or text/*). If a function, the type option is called as fn(req) and the request is parsed if it returns a truthy value. Defaults to text/plain.

verify

The verify option, if supplied, is called as verify(req, res, buf, encoding), where buf is a Buffer of the raw request body and encoding is the encoding of the request. The parsing can be aborted by throwing an error.

bodyParser.urlencoded([options])

Returns middleware that only parses urlencoded bodies and only looks at requests where the Content-Type header matches the type option. This parser accepts only UTF-8 encoding of the body and supports automatic inflation of gzip and deflate encodings.

A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body). This object will contain key-value pairs, where the value can be a string or array (when extended is false), or any type (when extended is true).

Options

The urlencoded function takes an optional options object that may contain any of the following keys:

extended

The extended option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true). The "extended" syntax allows for rich objects and arrays to be encoded into the URL-encoded format, allowing for a JSON-like experience with URL-encoded. For more information, please see the qs library.

Defaults to true, but using the default has been deprecated. Please research into the difference between qs and querystring and choose the appropriate setting.

inflate

When set to true, then deflated (compressed) bodies will be inflated; when false, deflated bodies are rejected. Defaults to true.

limit

Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. Defaults to '100kb'.

parameterLimit

The parameterLimit option controls the maximum number of parameters that are allowed in the URL-encoded data. If a request contains more parameters than this value, a 413 will be returned to the client. Defaults to 1000.

type

The type option is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function, type option is passed directly to the type-is library and this can be an extension name (like urlencoded), a mime type (like application/x-www-form-urlencoded), or a mime type with a wildcard (like */x-www-form-urlencoded). If a function, the type option is called as fn(req) and the request is parsed if it returns a truthy value. Defaults to application/x-www-form-urlencoded.

verify

The verify option, if supplied, is called as verify(req, res, buf, encoding), where buf is a Buffer of the raw request body and encoding is the encoding of the request. The parsing can be aborted by throwing an error.

depth

The depth option is used to configure the maximum depth of the qs library when extended is true. This allows you to limit the amount of keys that are parsed and can be useful to prevent certain types of abuse. Defaults to 32. It is recommended to keep this value as low as possible.

Errors

The middlewares provided by this module create errors using the http-errors module. The errors will typically have a status/statusCode property that contains the suggested HTTP response code, an expose property to determine if the message property should be displayed to the client, a type property to determine the type of error without matching against the message, and a body property containing the read body, if available.

The following are the common errors created, though any error can come through for various reasons.

content encoding unsupported

This error will occur when the request had a Content-Encoding header that contained an encoding but the "inflation" option was set to false. The status property is set to 415, the type property is set to 'encoding.unsupported', and the charset property will be set to the encoding that is unsupported.

entity parse failed

This error will occur when the request contained an entity that could not be parsed by the middleware. The status property is set to 400, the type property is set to 'entity.parse.failed', and the body property is set to the entity value that failed parsing.

entity verify failed

This error will occur when the request contained an entity that could not be failed verification by the defined verify option. The status property is set to 403, the type property is set to 'entity.verify.failed', and the body property is set to the entity value that failed verification.

request aborted

This error will occur when the request is aborted by the client before reading the body has finished. The received property will be set to the number of bytes received before the request was aborted and the expected property is set to the number of expected bytes. The status property is set to 400 and type property is set to 'request.aborted'.

request entity too large

This error will occur when the request body's size is larger than the "limit" option. The limit property will be set to the byte limit and the length property will be set to the request body's length. The status property is set to 413 and the type property is set to 'entity.too.large'.

request size did not match content length

This error will occur when the request's length did not match the length from the Content-Length header. This typically occurs when the request is malformed, typically when the Content-Length header was calculated based on characters instead of bytes. The status property is set to 400 and the type property is set to 'request.size.invalid'.

stream encoding should not be set

This error will occur when something called the req.setEncoding method prior to this middleware. This module operates directly on bytes only and you cannot call req.setEncoding when using this module. The status property is set to 500 and the type property is set to 'stream.encoding.set'.

stream is not readable

This error will occur when the request is no longer readable when this middleware attempts to read it. This typically means something other than a middleware from this module read the request body already and the middleware was also configured to read the same request. The status property is set to 500 and the type property is set to 'stream.not.readable'.

too many parameters

This error will occur when the content of the request exceeds the configured parameterLimit for the urlencoded parser. The status property is set to 413 and the type property is set to 'parameters.too.many'.

unsupported charset "BOGUS"

This error will occur when the request had a charset parameter in the Content-Type header, but the iconv-lite module does not support it OR the parser does not support it. The charset is contained in the message as well as in the charset property. The status property is set to 415, the type property is set to 'charset.unsupported', and the charset property is set to the charset that is unsupported.

unsupported content encoding "bogus"

This error will occur when the request had a Content-Encoding header that contained an unsupported encoding. The encoding is contained in the message as well as in the encoding property. The status property is set to 415, the type property is set to 'encoding.unsupported', and the encoding property is set to the encoding that is unsupported.

The input exceeded the depth

This error occurs when using bodyParser.urlencoded with the extended property set to true and the input exceeds the configured depth option. The status property is set to 400. It is recommended to review the depth option and evaluate if it requires a higher value. When the depth option is set to 32 (default value), the error will not be thrown.

Examples

Express/Connect top-level generic

This example demonstrates adding a generic JSON and URL-encoded parser as a top-level middleware, which will parse the bodies of all incoming requests. This is the simplest setup.

var express = require('express')
var bodyParser = require('body-parser')

var app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.use(function (req, res) {
  res.setHeader('Content-Type', 'text/plain')
  res.write('you posted:\n')
  res.end(JSON.stringify(req.body, null, 2))
})

Express route-specific

This example demonstrates adding body parsers specifically to the routes that need them. In general, this is the most recommended way to use body-parser with Express.

var express = require('express')
var bodyParser = require('body-parser')

var app = express()

// create application/json parser
var jsonParser = bodyParser.json()

// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })

// POST /login gets urlencoded bodies
app.post('/login', urlencodedParser, function (req, res) {
  res.send('welcome, ' + req.body.username)
})

// POST /api/users gets JSON bodies
app.post('/api/users', jsonParser, function (req, res) {
  // create user in req.body
})

Change accepted type for parsers

All the parsers accept a type option which allows you to change the Content-Type that the middleware will parse.

var express = require('express')
var bodyParser = require('body-parser')

var app = express()

// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }))

// parse some custom thing into a Buffer
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))

// parse an HTML body into a string
app.use(bodyParser.text({ type: 'text/html' }))

License

MIT