Node.js body parsing middleware for Express and Connect applications
npx @tessl/cli install tessl/npm-body-parser@2.2.0body-parser provides Node.js middleware for parsing HTTP request bodies in various formats including JSON, URL-encoded forms, raw data, and plain text. It integrates seamlessly with Express.js and other Connect-compatible frameworks, making parsed body data available through the req.body property.
npm install body-parserconst bodyParser = require('body-parser');For ES modules:
import bodyParser from 'body-parser';const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// Parse JSON bodies
app.use(bodyParser.json());
// Parse URL-encoded forms
app.use(bodyParser.urlencoded({ extended: false }));
// Parse raw bodies as Buffer
app.use(bodyParser.raw());
// Parse text bodies as string
app.use(bodyParser.text());
app.post('/data', (req, res) => {
console.log(req.body); // Parsed body data
res.send('Data received');
});body-parser is built around middleware functions that parse request bodies before they reach your handlers:
json(), raw(), text(), urlencoded()) that create Express-compatible middlewareParses JSON request bodies and makes the parsed data available as req.body. Supports Unicode encoding and automatic compression inflation.
function json(options?: {
inflate?: boolean;
limit?: number | string;
reviver?: (key: string, value: any) => any;
strict?: boolean;
type?: string | string[] | ((req: IncomingMessage) => boolean);
verify?: (req: IncomingMessage, res: ServerResponse, buf: Buffer, encoding: string) => void;
}): (req: IncomingMessage, res: ServerResponse, next: (err?: any) => void) => void;Parses URL-encoded form data and makes the parsed data available as req.body. Supports both simple and extended parsing modes.
function urlencoded(options?: {
extended?: boolean;
inflate?: boolean;
limit?: number | string;
parameterLimit?: number;
type?: string | string[] | ((req: IncomingMessage) => boolean);
verify?: (req: IncomingMessage, res: ServerResponse, buf: Buffer, encoding: string) => void;
defaultCharset?: string;
charsetSentinel?: boolean;
interpretNumericEntities?: boolean;
depth?: number;
}): (req: IncomingMessage, res: ServerResponse, next: (err?: any) => void) => void;Parses request bodies as raw Buffer data. Useful for handling binary data or when you need direct access to the raw request body.
function raw(options?: {
inflate?: boolean;
limit?: number | string;
type?: string | string[] | ((req: IncomingMessage) => boolean);
verify?: (req: IncomingMessage, res: ServerResponse, buf: Buffer, encoding: string) => void;
}): (req: IncomingMessage, res: ServerResponse, next: (err?: any) => void) => void;Parses request bodies as plain text strings. Supports configurable character encoding with utf-8 as the default.
function text(options?: {
defaultCharset?: string;
inflate?: boolean;
limit?: number | string;
type?: string | string[] | ((req: IncomingMessage) => boolean);
verify?: (req: IncomingMessage, res: ServerResponse, buf: Buffer, encoding: string) => void;
}): (req: IncomingMessage, res: ServerResponse, next: (err?: any) => void) => void;The generic bodyParser() function has been deprecated and will throw an error when called.
/**
* @deprecated Use individual middleware functions instead
* @throws {Error} Always throws error directing to use specific parsers
*/
function bodyParser(): never;Migration:
Instead of bodyParser(), use the specific parser functions:
bodyParser.json() for JSON bodiesbodyParser.urlencoded() for form databodyParser.raw() for raw buffersbodyParser.text() for text content// Common types used across all parsers
interface IncomingMessage {
headers: { [key: string]: string | string[] | undefined };
body?: any;
}
interface ServerResponse {
// Standard Node.js ServerResponse
}
// Error types that parsers may throw
interface BodyParserError extends Error {
status: number;
type: string;
body?: any;
charset?: string;
encoding?: string;
}All parsers can throw errors with specific status codes:
app.use(bodyParser.json());
app.use((err, req, res, next) => {
if (err.status === 400) {
return res.status(400).send('Bad JSON');
}
next(err);
});