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 / server-integration-patterns

Server integration patterns

Formidable connects an HTTP request body to application code that needs form fields and uploaded files. It accepts the raw Node request stream, chooses the parser for the request type, and returns the results through a callback, a promise, or events. This page focuses on the boundary between Formidable and a server or framework; see Form request lifecycle for the full request flow and IncomingForm API and events for the public parsing contract.

Integration model

Formidable is a low-level request parser rather than a complete web server. The server owns routing and the response, while Formidable owns reading the request body and producing fields and files.

The integration has one important boundary: pass the request stream that still contains the body. The parser consumes that stream and must run before another body parser or middleware drains it.

flowchart LR
  Client[HTTP client] --> Server[Node server or framework]
  Server --> Route[Upload route or middleware]
  Route --> Request[Raw Node request stream]
  Request --> Formidable[Formidable parser]
  Formidable --> Result[Fields and files]
  Result --> Handler[Application result or next middleware]
  Formidable --> Error[Parse or upload error]
  Error --> Handler

Request stream boundary

The request stream is the shared input for every integration style. Native Node handlers receive it as req; Koa handlers must pass ctx.req, not Koa's ctx.request wrapper. Express exposes the same Node request stream as req. form.parse() then reads the body until completion or failure.

References: README.md• L104-108 README.md• L152-156 README.md• L195-205 src/Formidable.js• L192-194

Result boundary

The route decides how to use the parsed result. A callback receives an error plus fields and files, while promise use returns those two result objects as an array. Event-based code can collect each field and file and send the response when the end event confirms that parsing and file writes are complete.

References: src/Formidable.js• L197-239 README.md• L412-426 examples/upload-multiple-files.js• L21-38

Native Node HTTP integration

The native pattern places Formidable inside the http.createServer() route that handles the upload. The handler checks the URL and method, creates one form for that request, parses req, and sends the response only after the parse result or error is known.

Upload route flow

A native handler keeps routing, parsing, and response work in a clear order. It can use the promise form for linear control flow or the callback form when that fits the surrounding event code.

  1. Match the upload path and HTTP method.
  2. Create a Formidable form with the options needed by the route.
  3. Pass the untouched req stream to form.parse().
  4. Return a client or server error when parsing fails.
  5. Serialize or process fields and files after parsing succeeds.

The response status can use the parser error's HTTP status when one is available. This keeps malformed or oversized requests from being reported as successful uploads.

References: examples/with-http.js• L5-18 examples/with-http.js• L51-63 README.md• L113-133

Event-based handlers

An event-based handler is useful when the application wants to observe each field or file as it arrives. The handler stores the observed values and waits for end before it closes the response, because a file event represents a completed file and the final event marks completion of the whole form.

References: examples/upload-multiple-files.js• L17-38 src/Formidable.js• L228-239 src/Formidable.js• L441-477

Framework request handlers

Framework integration keeps the framework's routing and response helpers while giving Formidable the underlying Node request stream. The main difference between frameworks is how the handler reports errors and how it waits for parsing to finish.

Express route handlers

An Express route can call Formidable directly because Express supplies a Node-compatible request stream. The route passes parse errors to Express with next(err) and lets the normal Express error path choose the final response; successful results can use a response helper such as res.json().

References: examples/with-express.js• L17-27 README.md• L152-193

Koa middleware

Koa middleware must adapt Formidable's completion signal to Koa's promise-based flow. It passes ctx.req to form.parse(), rejects the middleware promise on parse error, and stores or formats the result on ctx.state or ctx.body before control moves to later middleware.

References: examples/with-koa2.js• L10-40 README.md• L217-239

Middleware ordering

Formidable and another request-body reader should not process the same body stream in sequence. Put the upload middleware on the route that owns the upload and run it before any middleware that consumes the request body; after parsing, later middleware can use the fields and files placed in the framework context or passed through application state.

References: examples/with-koa2.js• L10-40 test/…/custom-plugins.test.js• L16-25 README.md• L203-205

Multiple-file upload patterns

Multiple-file uploads are represented by the form field name that the client sends. A client can send several files under one field, use several file fields, or send a directory while preserving relative names.

Repeated file fields

An HTML file input with multiple sends more than one file for the same field name. Formidable groups each completed file under that name in the files result, so application code must treat a file value as a collection rather than assume one file per field. The same grouping also supports repeated file parts from clients that do not use an HTML form.

References: examples/with-http.js• L69-73 examples/with-express.js• L9-13 src/Formidable.js• L228-233

Directory upload requests

A browser directory upload sends relative path information with each file. An application that wants to keep that structure enables directory creation and supplies a naming rule that returns a path below the upload root; applications that do not need the structure can use a flat naming rule instead.

References: examples/with-http.js• L13-40 README.md• L351-357 src/Formidable.js• L640-655

Field and file collection

The callback and promise results are grouped for application use, while event-based handlers can build their own collections as parts arrive. Multipart and URL-encoded fields may also repeat, so code that maps form data into a single-value model must make that choice explicitly.

References: src/Formidable.js• L214-233 README.md• L734-748 examples/upload-multiple-files.js• L18-35

Upload destination handoff

The server integration can end at local storage or at an application-owned writable stream. This lets a route use the same request and result flow when files must go to a local file system, object storage, or another service.

Local and external destinations

By default, uploaded files are written below the configured upload directory. A custom write-stream handler changes the destination contract: the application supplies a writable stream for each file, and Formidable reports the file after that stream finishes. This keeps storage selection in the route or service layer without changing how the request is parsed.

References: src/Formidable.js• L558-588 src/PersistentFile.js• L36-41 src/VolatileFile.js• L37-42 examples/store-files-on-s3.js• L15-41

File naming boundary

The application can control the stored name through the naming option. Names are resolved below the upload directory, and unsafe paths are replaced with the configured invalid name. A route that accepts client paths should therefore define a safe naming policy rather than trust the original filename.

References: examples/with-http.js• L20-40 src/Formidable.js• L640-671 README.md• L351-365

Completion and error boundaries

The server must connect Formidable's completion and error signals to its own response lifecycle. A successful response belongs after parsing completes; an error response belongs on the first parse, stream, upload, or request-abort failure.

Completion state

Formidable delays the final end signal while file output is still being flushed. This matters most for multiple uploads and external streams: the application can use the final callback, promise resolution, or end event as the point at which all returned files are ready for downstream work.

References: src/Formidable.js• L441-477 src/Formidable.js• L723-729 examples/upload-multiple-files.js• L30-38

Failure response

Request errors, aborted connections, unsupported content types, and upload limit failures reach the parse error path. A framework handler should pass or translate that error once, and a native handler should end the response without later writing a success response.

References: src/Formidable.js• L262-270 src/Formidable.js• L302-320 src/Formidable.js• L527-538 README.md• L119-128

Change checklist

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

  • A change to the request argument or body-reading order must stay consistent across native handlers, Express routes, and Koa middleware.
  • A change to result grouping must preserve the contract for repeated file names and for event-based collection.
  • A change to completion timing must keep response writes after local or external file streams finish.
  • A change to upload naming or directory handling must preserve the boundary between client-supplied paths and the upload root.
  • A change to parse errors must keep callback, promise, event, and framework error paths aligned.

Important failure modes

TriggerConsequenceGuard
A body parser or middleware reads the request before FormidableFormidable receives an empty or incomplete bodyRun Formidable first on the upload route and pass the raw Node request stream.
Koa passes ctx.request instead of ctx.reqFormidable cannot consume the expected Node request streamUse ctx.req at the integration boundary.
The handler sends success before parsing or file output endsThe response can report success for an incomplete uploadRespond from the parse completion callback, promise, or end event.
The handler ignores the parse error and continues its success pathA failed, aborted, or rejected upload can produce a false success responseReturn after the error path or pass the error to framework middleware.
Client paths are used without a safe naming ruleFiles can be placed outside the intended upload area or lose their intended structureDefine a naming policy and keep resolved paths below the upload directory.

Key files

FileWhy to read it
examples/with-http.jsShows the native Node route pattern, route checks, response handling, naming, and directory upload form.
examples/with-express.jsShows direct use from an Express route and handoff of parse errors to next.
examples/with-koa2.jsShows the Koa request boundary, promise adaptation, state handoff, and middleware continuation.
examples/upload-multiple-files.jsShows event-based collection for repeated file fields and response timing.
examples/store-files-on-s3.jsShows how a server route sends file data to an application-owned external stream.
src/Formidable.jsDefines the shared parse boundary, result grouping, completion state, and request failure behavior.
src/index.jsDefines the package entry point used by server and framework handlers.