What is formidable?
The formidable npm package is a Node.js module for parsing form data, especially file uploads. It can handle multipart/form-data, which is used for uploading files through forms.
What are formidable's main functionalities?
File Upload
This code creates an HTTP server that listens for POST requests on the '/upload' path. It uses formidable to parse the incoming form data and handle file uploads.
const formidable = require('formidable');
const http = require('http');
http.createServer((req, res) => {
if (req.url === '/upload' && req.method.toLowerCase() === 'post') {
const form = new formidable.IncomingForm();
form.parse(req, (err, fields, files) => {
if (err) {
res.writeHead(500, { 'content-type': 'text/plain' });
res.end('Error parsing the files');
return;
}
res.writeHead(200, { 'content-type': 'text/plain' });
res.write('Files uploaded successfully:\n');
res.end(JSON.stringify(files, null, 2));
});
}
}).listen(8080);
Form Field Parsing
This code snippet demonstrates how to use formidable to parse regular form fields (text inputs, selects, etc.) in addition to file uploads.
const formidable = require('formidable');
const http = require('http');
http.createServer((req, res) => {
if (req.url === '/submit' && req.method.toLowerCase() === 'post') {
const form = new formidable.IncomingForm();
form.parse(req, (err, fields, files) => {
if (err) {
res.writeHead(500, { 'content-type': 'text/plain' });
res.end('Error parsing the form fields');
return;
}
res.writeHead(200, { 'content-type': 'text/plain' });
res.write('Form fields submitted:\n');
res.end(JSON.stringify(fields, null, 2));
});
}
}).listen(8080);
File Upload Progress
This example shows how to track the progress of a file upload using formidable's 'progress' event, which provides the bytes received and the total bytes expected.
const formidable = require('formidable');
const http = require('http');
http.createServer((req, res) => {
if (req.url === '/upload' && req.method.toLowerCase() === 'post') {
const form = new formidable.IncomingForm();
form.on('progress', (bytesReceived, bytesExpected) => {
console.log(`Progress: ${bytesReceived}/${bytesExpected}`);
});
form.parse(req, (err, fields, files) => {
// Handle file upload and response
});
}
}).listen(8080);
Other packages similar to formidable
multer
Multer is another popular Node.js middleware for handling multipart/form-data, which is primarily used for uploading files. It is built on top of busboy for maximum efficiency. Unlike formidable, multer is specifically designed for use with Express applications and includes more options for file storage and manipulation.
busboy
Busboy is a low-level Node.js module for parsing multipart/form-data request bodies. Formidable is actually built on top of busboy. Busboy is faster and more efficient but requires more setup and manual handling compared to formidable, which provides a higher-level API.
multiparty
Multiparty is a Node.js module for parsing multipart/form-data requests. It is similar to formidable in terms of functionality but has a different API and is known for being more memory efficient, as it streams files to disk instead of buffering them in memory.
Formidable
Purpose
A node.js module for parsing form data, especially file uploads.
Current status
This module was developed for Transloadit, a service focused on uploading
and encoding images and videos. It has been battle-tested against hundreds of GB of file uploads from
a large variety of clients and is considered production-ready.
Features
- Fast (~500mb/sec), non-buffering multipart parser
- Automatically writing file uploads to disk
- Low memory footprint
- Graceful error handling
- Very high test coverage
Changelog
v1.0.6
- Do not default to the default to the field name for file uploads where
filename="".
v1.0.5
- Support filename="" in multipart parts
- Explain unexpected end() errors in parser better
Note: Starting with this version, formidable emits 'file' events for empty
file input fields. Previously those were incorrectly emitted as regular file
input fields with value = "".
v1.0.4
- Detect a good default tmp directory regardless of platform. (#88)
v1.0.3
- Fix problems with utf8 characters (#84) / semicolons in filenames (#58)
- Small performance improvements
- New test suite and fixture system
v1.0.2
- Exclude node_modules folder from git
- Implement new
'aborted'
event - Fix files in example folder to work with recent node versions
- Make gently a devDependency
See Commits
v1.0.1
- Fix package.json to refer to proper main directory. (#68, Dean Landolt)
See Commits
v1.0.0
- Add support for multipart boundaries that are quoted strings. (Jeff Craig)
This marks the beginning of development on version 2.0 which will include
several architectural improvements.
See Commits
v0.9.11
- Emit
'progress'
event when receiving data, regardless of parsing it. (Tim Koschützki) - Use W3C FileAPI Draft properties for File class
Important: The old property names of the File class will be removed in a
future release.
See Commits
Older releases
These releases were done before starting to maintain the above Changelog:
Installation
Via npm:
npm install formidable@latest
Manually:
git clone git://github.com/felixge/node-formidable.git formidable
vim my.js
# var formidable = require('./formidable');
Note: Formidable requires gently to run the unit tests, but you won't need it for just using the library.
Example
Parse an incoming file upload.
var formidable = require('formidable'),
http = require('http'),
sys = require('sys');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(sys.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}).listen(80);
API
formidable.IncomingForm
new formidable.IncomingForm()
Creates a new incoming form.
incomingForm.encoding = 'utf-8'
The encoding to use for incoming form fields.
incomingForm.uploadDir = process.env.TMP || '/tmp' || process.cwd()
The directory for placing file uploads in. You can move them later on using
fs.rename()
. The default directory is picked at module load time depending on
the first existing directory from those listed above.
incomingForm.keepExtensions = false
If you want the files written to incomingForm.uploadDir
to include the extensions of the original files, set this property to true
.
incomingForm.type
Either 'multipart' or 'urlencoded' depending on the incoming request.
incomingForm.maxFieldsSize = 2 * 1024 * 1024
Limits the amount of memory a field (not file) can allocate in bytes.
If this value is exceeded, an 'error'
event is emitted. The default
size is 2MB.
incomingForm.bytesReceived
The amount of bytes received for this form so far.
incomingForm.bytesExpected
The expected number of bytes in this form.
incomingForm.parse(request, [cb])
Parses an incoming node.js request
containing form data. If cb
is provided, all fields an files are collected and passed to the callback:
incomingForm.parse(req, function(err, fields, files) {
// ...
});
incomingForm.onPart(part)
You may overwrite this method if you are interested in directly accessing the multipart stream. Doing so will disable any 'field'
/ 'file'
events processing which would occur otherwise, making you fully responsible for handling the processing.
incomingForm.onPart = function(part) {
part.addListener('data', function() {
// ...
});
}
If you want to use formidable to only handle certain parts for you, you can do so:
incomingForm.onPart = function(part) {
if (!part.filename) {
// let formidable handle all non-file parts
incomingForm.handlePart(part);
}
}
Check the code in this method for further inspiration.
Event: 'progress' (bytesReceived, bytesExpected)
Emitted after each incoming chunk of data that has been parsed. Can be used to roll your own progress bar.
Event: 'field' (name, value)
Emitted whenever a field / value pair has been received.
Event: 'fileBegin' (name, file)
Emitted whenever a new file is detected in the upload stream. Use this even if
you want to stream the file to somewhere else while buffering the upload on
the file system.
Event: 'file' (name, file)
Emitted whenever a field / file pair has been received. file
is an instance of File
.
Event: 'error' (err)
Emitted when there is an error processing the incoming form. A request that experiences an error is automatically paused, you will have to manually call request.resume()
if you want the request to continue firing 'data'
events.
Event: 'aborted'
Emitted when the request was aborted by the user. Right now this can be due to a 'timeout' or 'close' event on the socket. In the future there will be a separate 'timeout' event (needs a change in the node core).
Event: 'end' ()
Emitted when the entire request has been received, and all contained files have finished flushing to disk. This is a great place for you to send your response.
formidable.File
file.size = 0
The size of the uploaded file in bytes. If the file is still being uploaded (see 'fileBegin'
event), this property says how many bytes of the file have been written to disk yet.
file.path = null
The path this file is being written to. You can modify this in the 'fileBegin'
event in
case you are unhappy with the way formidable generates a temporary path for your files.
file.name = null
The name this file had according to the uploading client.
file.type = null
The mime type of this file, according to the uploading client.
file.lastModifiedDate = null
A date object (or null
) containing the time this file was last written to. Mostly
here for compatibility with the W3C File API Draft.
License
Formidable is licensed under the MIT license.
Ports
Credits