Busboy is a high-performance, streaming parser for incoming HTML form data in Node.js, specifically designed to handle `multipart/form-data` and `application/x-www-form-urlencoded` request bodies. As of version 1.6.0, it provides a lightweight and memory-efficient solution for processing file uploads and form fields by emitting events as data streams in, rather than buffering the entire request. Its event-driven API grants developers fine-grained control over how incoming data is managed, making it suitable for integration with various storage solutions or processing pipelines. While a precise release cadence isn't publicly defined, the project appears actively maintained. Busboy differentiates itself by focusing purely on parsing the raw input stream without making assumptions about how the parsed data should be stored or handled, leaving those decisions entirely to the application developer. It requires Node.js v10.16.0 or newer.
npm install busboyVerified import paths — ran on the pinned version, not inferred.
This example sets up a basic HTTP server that uses Busboy to parse multipart/form-data POST requests. It demonstrates how to listen for 'file' and 'field' events to process incoming file uploads and regular form fields, logging their details and data lengths.
Migrate `new Busboy(...)` calls to `busboy(...)`. Review event signatures for 'file' and 'field' events, as their arguments were consolidated into an `info` object. Additionally, the default for `preservePath` changed to `false` for multipart files.
For `file` events, instead of `(name, file, filename, encoding, mimetype)`, use `(name, file, info)` and access properties like `info.filename`. For `field` events, instead of `(name, val, fieldnameTruncated, valTruncated, encoding, mimetype)`, use `(name, val, info)` and access properties like `info.nameTruncated`.
Always attach 'data' and 'end'/'close' listeners to the `file` stream, or pipe it to a destination (e.g., `fs.createWriteStream`). If a file stream is not consumed, ensure `file.resume()` is called to prevent the parser from stalling.
Always sanitize `info.filename` before using it to construct a file path. Use a library like `path.basename()` or implement a robust sanitation method to ensure the path does not escape the intended directory. For example, `path.join(uploadDir, path.basename(info.filename))`.
Configure `limits` in the Busboy options, such as `limits: { fileSize: 10 * 1024 * 1024, files: 5, fields: 10 }` to restrict maximum file size, number of files, and number of fields, respectively. Also, implement timeouts for inactive connections.Remove the `new` keyword: `const bb = busboy({ headers: req.headers });`Ensure the package is installed in your project: `npm install busboy` or `yarn add busboy`.
Use the CommonJS `require` syntax: `const busboy = require('busboy');`No dependency data recorded yet.