IncomingForm API and events
Formidable exposes a Node.js request parser through an IncomingForm instance. An application creates the instance, gives it a request stream, and receives parsed fields and uploaded-file records through a callback or a promise. The instance also emits progress and lifecycle events so an application can observe or control the request while parsing. For the end-to-end request flow, see Form request lifecycle; for limits and error handling, see Request options, limits, and errors.
Package entry points
Public exports
The package entry point is src/index.js. Its default export is a factory named formidable; the factory creates an IncomingForm without requiring the caller to use new. The named IncomingForm export is an alias for the same form class, and Formidable exposes that class under its class name.
The entry point also exports the file record types and the built-in parser and plugin names. Most applications need only the default factory and, when handling failures, the errors export.
| Export | Purpose |
|---|---|
formidable default export | Creates an IncomingForm instance from options. |
formidable named export | The same factory as the default export. |
IncomingForm / Formidable | Access to the form class. |
File / PersistentFile / VolatileFile | File record types used for uploaded data. |
errors | Internal error codes used with FormidableError instances. |
References: src/index.js• L1-27 package.json• L9-36
Parsing API
Form creation and request parsing
An application creates one form for a request and passes the Node.js request stream to parse. The form reads request headers first, selects a handler for the content type, and then turns incoming data into field events and file events. Pass options to the factory or constructor when the form is created.
import formidable from "formidable";
const form = formidable({});
const [fields, files] = await form.parse(req);
parse(req, callback) uses the Node-style callback shape (error, fields, files). Without a callback, parse(req) returns a promise that resolves to [fields, files] or rejects with the parsing error. The callback and the promise settle once only. The callback form returns the form instance after parsing starts, while the promise form is the result to await.
References: src/index.js• L3-23 src/Formidable.js• L94-100 src/Formidable.js• L192-300 test-node/…/promise.test.js• L29-43
Request stream control
pause() and resume() act on the request stream that the form is parsing. Formidable uses this boundary while it waits for file writes, but callers can also use it when they need to coordinate request consumption with their own work. A stream failure or a request abort can make these methods return false instead of changing the stream.
onPart(part) is the hook for applications that need to handle parsed parts themselves. The default behavior sends fields and files through the normal result and event flow. Replacing this hook transfers responsibility for the selected parts to the application, so the normal field and file processing does not occur unless the custom hook invokes the built-in part handler.
References: src/Formidable.js• L163-190 src/Formidable.js• L342-345 src/Formidable.js• L347-479 README.md• L428-431 README.md• L581-611
Lifecycle events
Parsing events
The form is an EventEmitter. Register listeners before calling parse when the listener must observe the first progress update or any early parsing failure. Events describe the request as it moves from received bytes to completed results.
The main parsing events are:
| Event | Arguments | Meaning |
|---|---|---|
progress | bytesReceived, bytesExpected | Formidable has received another request chunk. bytesExpected comes from the request length when it is known. |
field | name, value | A non-file form field is complete. |
fileBegin | formName, file | A file has been identified and its file record is ready. The record can still be adjusted at this point. |
file | formName, file | The file has finished writing and is available in the result. |
end | none | The request has ended and all pending file writes have finished. |
The normal completion relationship is fileBegin before file, followed by end after the last file is flushed. Field events can occur between progress updates as the parser completes each field.
References: src/Formidable.js• L217-240 src/Formidable.js• L323-339 src/Formidable.js• L389-391 src/Formidable.js• L413-415 src/Formidable.js• L441-477 src/Formidable.js• L723-729
Failure events
error signals that Formidable cannot complete the request. The error can include an internal code and an HTTP-oriented httpCode, which lets the server map the failure to its response policy. The callback receives the same error, and the promise returned by parse rejects with it.
aborted signals that the client or connection stopped the request. Formidable emits error after aborted, and an aborted request does not reach normal end completion. Treat the error path as final for the form and do not use partial fields or files as a successful result.
References: src/Formidable.js• L235-240 src/Formidable.js• L263-270 src/Formidable.js• L527-539 src/FormidableError.js• L21-27 test/…/connection-aborted.test.js• L25-45
File progress events
Each file record is also an event source. It emits progress with the number of bytes written to that file and emits end when its write stream is closed. Applications that need per-file progress can listen to the file received in fileBegin; applications that need request-level progress should listen to the form's progress event instead.
References: src/PersistentFile.js• L63-89 src/VolatileFile.js• L66-90 README.md• L670-697
Result contract
Fields and files maps
The completed parse result is a pair of maps: fields contains non-file values and files contains uploaded-file records. Field names and file names are the keys. Repeated multipart or URL-encoded field names are represented as arrays. File entries are always arrays because one form name can receive more than one upload. JSON bodies provide their parsed value as the fields result and do not create file entries.
The maps use null prototypes. Treat them as data maps rather than objects with inherited methods, and use their own keys when checking whether a name was supplied.
form.parse(req, (error, fields, files) => {
if (error) return next(error);
// fields.title may be a string or an array for repeated form fields.
// files.photos is an array of file records.
res.json({ fields, files });
});
References: src/Formidable.js• L213-233 src/…/querystring.js• L23-37 src/…/json.js• L24-38 src/…/octetstream.js• L65-72
File records
The file value is a file record that describes the uploaded content and where it is available. The record includes the client name, the generated or chosen stored name, the MIME type, the byte size, and the file path when the chosen storage mode has a path. A configured hash is exposed after the file is complete. toJSON() provides a safe summary for logging or a JSON response.
The fileBegin event exposes the record before all bytes are written. The file event and the files result expose it after the write is complete, so size and hash values are then final. A file may be disk-backed or use a caller-provided write stream; the result contract stays centered on the file record rather than on the storage destination.
References: src/PersistentFile.js• L7-57 src/VolatileFile.js• L6-59 src/Formidable.js• L404-415 src/Formidable.js• L473-477 README.md• L613-649
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the default factory,
IncomingFormalias, and named class exports aligned when the form constructor or public entry point changes. - Keep callback errors, promise rejection, and
errorevent behavior aligned when parsing failure paths change. - Preserve the
fileBegintofiletoendordering when file flushing or request completion changes. - Update consumers that assume repeated fields are arrays and file map values are arrays when result assembly changes.
- Recheck event listener timing when header parsing or the first
progressevent changes.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
parse receives a request whose content type has no enabled handler | The form emits error; the callback fails or the promise rejects. | Enable the handler for the request type or reject the request before parsing. |
The request emits aborted | Formidable emits aborted, then error; normal end is not emitted. | Handle both events through the error path and do not send a success response from end. |
An error listener is attached after parsing starts | An early error or progress event can be missed by the application. | Register listeners before parse(req). |
A custom onPart handler does not pass a part to the default handler | That part does not produce the normal field or file result. | Make the custom hook handle the part completely or delegate selected parts to the built-in handler. |
| A caller treats every field as a scalar | Repeated multipart or URL-encoded names lose values or produce incorrect application data. | Read repeated field values as arrays. |
Key files
| File | Why to read it |
|---|---|
| src/index.js | Defines the package's main factory and public class, file, parser, plugin, and error exports. |
| src/Formidable.js | Defines the IncomingForm API, result assembly, event contract, and request completion behavior. |
| src/PersistentFile.js | Shows the public shape and completion behavior of the default file record. |
| src/VolatileFile.js | Shows the public shape of a file record backed by a caller-provided stream. |
| src/…/querystring.js and src/…/json.js | Show how structured request bodies become fields. |
| src/…/multipart.js and src/…/octetstream.js | Show how file and field events enter the common result contract. |
| README.md• L412-431 and README.md• L651-729 | Provides the user-facing parse and event examples. |