Introducing a new way to review your PRs.

Read the announcement

node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.

View on GitHub

13 documents · Updated Nov 9, 2026

docs / form-request-lifecycle

Form request lifecycle

Formidable turns a Node.js HTTP request stream into a pair of field and file maps. An application creates and configures one IncomingForm instance, passes the request to parse, and receives a result after the body is read and accepted file data is complete. This page gives the end-to-end view; see IncomingForm API and events for the public contract and Request options, limits, and errors for option and failure details.

Request intake

Form instance configuration

An IncomingForm instance holds the rules for one parse operation. Its options select the accepted content types, field and file limits, file naming, and file destination before any request bytes are consumed. The default factory and the IncomingForm class create the same kind of instance.

The main configuration groups are:

Configuration groupRole in the lifecycle
Parser pluginsDecide which request body types the form can accept.
Field and file limitsStop input that exceeds the application policy.
File naming and destinationChoose the name and place used for accepted file data.
File filter and empty-file rulesDecide which file parts enter the file path.
Hash and write-stream settingsAdd a digest or send file bytes to an application-owned stream.

Create a new form for each request. Options are copied into the instance at construction time, and the instance then prepares its plugin list and file naming behavior.

References: src/Formidable.js• L33-53 src/Formidable.js• L94-150 src/index.js• L3-23 README.md• L298-357

Request stream handoff

parse receives the Node.js request stream and installs its result and error listeners before it starts consuming data. It first reads the request headers. After header checks select a parser, the request body flows through a decoding step and then into the form parser.

The request-level sequence is:

flowchart LR
    A[HTTP request stream] --> B[Create IncomingForm]
    B --> C[Read headers]
    C --> D[Select parser]
    D --> E[Decode content encoding]
    E --> F[Feed body chunks]
    F --> G[Emit field and file results]
    G --> H[Callback or promise]

The form tracks received bytes and expected bytes when the request provides a content length. It also watches request errors and aborts so that transport failures use the same final error path as parser failures.

References: src/Formidable.js• L192-300 src/Formidable.js• L302-340 src/Formidable.js• L263-294

Content type routing

Header-based parser selection

The request Content-Type determines how the body becomes form data. During header processing, each enabled plugin checks whether it understands that media type and attaches the matching parser. The default set supports multipart data, URL-encoded forms, JSON bodies, and raw octet streams.

An empty body uses a no-data path and can complete without a content type. A non-empty body without a content type fails. A body with a content type that has no enabled parser also fails, so the enabled plugin set is part of the request acceptance contract.

References: src/Formidable.js• L482-525 src/…/index.js• L1-6 src/…/multipart.js• L17-36 src/…/querystring.js• L14-17 src/…/json.js• L14-18 src/…/octetstream.js• L14-17

Body decoding

Content encoding is separate from content type. When the request declares gzip, deflate, br, or compress, Formidable places a decompression stream before the selected parser. With no supported compression value, it passes the request bytes through unchanged.

This separation lets the content-type parser focus on the body format. The parser receives decoded chunks through the same write path in every case.

References: src/Formidable.js• L242-295 src/Formidable.js• L323-339

Body format paths

Each supported body format enters the common result flow through a format-specific parser. Multipart and URL-encoded input can produce repeated field names. JSON supplies a parsed value as fields, while an octet stream represents the request body as one file.

Body formatPrimary result
multipart/form-data or multipart/relatedFields and one or more file parts
application/x-www-form-urlencodedField pairs
JSON media typeParsed fields value
application/octet-streamOne raw file

References: src/…/multipart.js• L42-54 src/…/querystring.js• L23-37 src/…/json.js• L24-38 src/…/octetstream.js• L23-47 src/…/index.js• L1-15

Part processing

Field production

A field is a body value without a file media type. The form collects the field when its part ends. Multipart and URL-encoded fields with the same name become arrays; other field paths keep the value supplied by their parser.

Field size and count rules apply as fields arrive. A rejected field stops successful completion rather than returning a result that appears complete.

References: src/Formidable.js• L213-227 src/Formidable.js• L347-393 src/Formidable.js• L687-703 src/…/querystring.js• L28-35 src/…/json.js• L29-35

File production

A file part carries a form name, client filename, and media type. The form applies its file filter, chooses a stored name and safe destination, creates a file record, and emits fileBegin before it accepts the file bytes. The bytes then go to the default local writer or to an application-provided write stream.

The completed file is emitted only after its writer finishes. The form stores file records by form name, so each file entry is an array even when one file was supplied for that name. See File lifecycle and storage for the storage-focused view.

References: src/Formidable.js• L342-479 src/Formidable.js• L558-588 src/…/multipart.js• L54-165 src/…/octetstream.js• L23-81 src/PersistentFile.js• L7-89 src/VolatileFile.js• L6-90

Part flow control

Formidable pauses the request while a file writer handles a buffer, then resumes the request after the write callback. Multipart part handling also pauses the parser while the form decides how to process the part. These boundaries keep parser progress tied to the ability of the selected destination to accept data.

Applications can replace onPart when they need to own part handling. That choice moves responsibility for the selected part away from the default field and file result path.

References: src/Formidable.js• L163-190 src/Formidable.js• L342-345 src/Formidable.js• L399-439 src/…/multipart.js• L95-165 README.md• L581-611

Result completion

Completion boundary

The parser marks the request as ended when it has consumed the body. The form does not emit its final end event until no file writes remain and no error has occurred. This makes end the point at which the field and file results are ready for normal use.

The normal completion flow is:

sequenceDiagram
    participant Request
    participant Form as IncomingForm
    participant Parser
    participant Writer
    participant App

    Request->>Form: Headers and body chunks
    Form->>Parser: Select and feed parser
    Parser->>Form: Field or file part
    Form->>Writer: Accept file bytes
    Writer-->>Form: File write complete
    Form-->>App: file event
    Parser-->>Form: Body ended
    Form-->>App: end event
    Form-->>App: fields and files result

References: src/Formidable.js• L228-240 src/Formidable.js• L441-477 src/Formidable.js• L723-729 src/…/querystring.js• L32-35 src/…/json.js• L33-35 src/…/octetstream.js• L65-81

Result delivery

The callback form of parse receives (error, fields, files). Without a callback, parse returns a promise that resolves to [fields, files] or rejects with the same error delivered through the form's error event. The form assembles these maps from field and file events and settles the result once.

Use the result only on the success path. On failure, opened file writers are destroyed and normal end completion is not a success signal.

References: src/Formidable.js• L192-240 src/Formidable.js• L527-539 README.md• L412-426 src/FormidableError.js• L21-27

Lifecycle observation

The form exposes request progress and part completion through events. Register listeners before parse so the application can observe early progress or errors. The key ordering rule is that fileBegin identifies a file before writing, file publishes it after writing, and end follows all completed files.

For the full event arguments and public methods, see IncomingForm API and events. For limits, status codes, and error codes, see Request options, limits, and errors.

References: src/Formidable.js• L217-240 src/Formidable.js• L263-270 src/Formidable.js• L323-339 src/Formidable.js• L399-477 README.md• L651-729

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep header routing, parser setup, and request stream handoff in one valid order. Request data can arrive as soon as parsing starts.
  • Keep parser completion and file-writer completion coupled. Emitting end early can expose incomplete files.
  • Keep field aggregation rules aligned with the form type. A type change can alter whether repeated field names are arrays.
  • Keep callback errors, promise rejection, and the error event on the same final path.
  • Keep custom onPart behavior aligned with the result contract. A part that is not delegated or handled will not appear in the normal result.
  • Keep file cleanup aligned with the selected writer. Local files and application-owned destinations do not share the same cleanup owner.

Important failure modes

TriggerConsequenceGuard
A non-empty request has no content typeParsing fails before a body parser can handle the request.Send a supported Content-Type header.
No enabled plugin matches the content typeThe form reports an unsupported parser error and the parse result fails.Enable the matching built-in plugin or add a custom one.
Multipart content has no boundary or ends in an invalid stateThe multipart parser reports a client error and normal completion stops.Send a complete multipart body with its boundary.
Field count or combined field data exceeds its limitThe form emits an error and does not complete successfully.Set limits that match the request policy.
A file is filtered, empty when empty files are not allowed, or outside its size limitsThe file is skipped or the request fails, depending on the rule.Make file policy explicit and handle the parse error.
The request is aborted or a writer failsFormidable emits the failure path and destroys opened file writers.Treat error as final and make external writers clean up their own destination.
A custom onPart handler does not delegate a partThe part does not enter the normal fields or files maps.Handle the part fully or call the default part handler.

Key files

FileWhy to read it
src/Formidable.jsDefines form configuration, request intake, parser setup, result assembly, completion, and cleanup.
src/index.jsShows the factory and class names used to create a form.
src/…/index.jsShows the default body-format handlers.
src/…/multipart.jsShows how multipart headers and parts enter the shared field and file flow.
src/…/querystring.js, src/…/json.js, and src/…/octetstream.jsShow the three non-multipart paths into fields or a file.
src/PersistentFile.js and src/VolatileFile.jsShow the two writer choices that control file completion and cleanup.
src/FormidableError.jsDefines the error shape and lifecycle error codes.
README.md• L298-426 and README.md• L651-729Gives the user-facing configuration, parse, file, and event contract.