body-parser is a Node.js middleware for Express that parses incoming request bodies, making them available under the `req.body` property. It currently maintains two major stable versions: `v2.2.2` (for Node.js 18+) and `v1.20.4` (for older Node.js versions), with regular updates addressing security and dependency concerns.
npm install body-parserVerified import paths — ran on the pinned version, not inferred.
This Express.js application demonstrates how to integrate `body-parser` to handle both JSON and URL-encoded form data. It shows setting up the middleware globally and accessing the parsed data via `req.body` in route handlers.
Upgrade your Node.js environment to v18 or a more recent LTS version, or remain on `body-parser` v1.x if bound to an older Node.js runtime.
Upgrade `body-parser` to `v2.2.1` or `v1.20.4` (or newer) immediately to mitigate the vulnerability.
Replace `app.use(bodyParser())` with specific parsers, e.g., `app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true }));`If your application relies on deeply nested URL-encoded data, explicitly set the `depth` option, e.g., `bodyParser.urlencoded({ extended: true, depth: Infinity })`.Always validate the shape and types of properties in `req.body` before using them, e.g., `if (typeof req.body.foo !== 'string') { /* handle error */ }`.For `multipart/form-data`, use dedicated middleware like `multer`, `busboy`, `multiparty`, or `formidable`.
Ensure the client sends the correct `Content-Type` header for the body being sent. If `req.body` is unexpectedly empty, check the `Content-Type` header in the incoming request.
Run `npm install body-parser` or `yarn add body-parser` in your project directory.
Increase the `limit` option for the specific parser, e.g., `app.use(bodyParser.json({ limit: '5mb' }))`.Verify the client's `Content-Type` header (e.g., `application/json`, `application/x-www-form-urlencoded`). Ensure `app.use(bodyParser.json())` (or other parsers) is placed before your route handlers. Also, confirm the request actually contains a body.
Ensure the client sends a well-formed JSON string. If the content is not JSON, use the appropriate parser (e.g., `bodyParser.urlencoded()` or `bodyParser.text()`).
Implement robust validation for `req.body` properties before accessing them, e.g., `if (req.body && typeof req.body.someProp === 'string') { /* handle missing/invalid prop */ } else { /* handle missing/invalid prop */ }`.No dependency data recorded yet.