Form Data Middleware
Form body parsing middleware that parses incomingFormData and exposes it via context.get(FormData).
Installation
Function
formData()
Creates middleware that parses FormData from the request body and populates request context.
Signature:
options- Optional form data parsing settings
Options
FormDataOptions
suppressErrors
Set true to suppress parse errors. When enabled, invalid form data will result in an empty FormData object instead of throwing an error.
- Type:
boolean - Default:
false
uploadHandler
A function that handles file uploads. It receives a FileUpload object and may return any value that is a valid FormData value. Default is undefined, which means file uploads are stored in memory.
- Type:
FileUploadHandler - Default:
undefined
Basic Usage
File Uploads
Uploaded files are available in the parsedFormData object. For a single file field, use formData.get(name). For repeated file fields, use formData.getAll(name).
Custom Upload Handler
You can use a custom upload handler to customize how file uploads are handled. The return value of the upload handler will be used as the value of the form field in theFormData object.
Suppress Errors
Some requests may contain invalid form data that cannot be parsed. You can suppress parse errors by settingsuppressErrors to true.
Multiple File Upload
Types
FileUpload
FormDataParseError
Thrown when form data parsing fails (unless suppressErrors is true).
Related
- fetch-router - Router for the web Fetch API
- form-data-parser - The underlying form data parser