or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdjson-parser.mdraw-parser.mdtext-parser.mdurlencoded-parser.md
tile.json

tessl/npm-body-parser

Node.js body parsing middleware for Express and Connect applications

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/body-parser@2.2.x

To install, run

npx @tessl/cli install tessl/npm-body-parser@2.2.0

index.mddocs/

body-parser

body-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.

Package Information

  • Package Name: body-parser
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install body-parser

Core Imports

const bodyParser = require('body-parser');

For ES modules:

import bodyParser from 'body-parser';

Basic Usage

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');
});

Architecture

body-parser is built around middleware functions that parse request bodies before they reach your handlers:

  • Parser Factories: Functions (json(), raw(), text(), urlencoded()) that create Express-compatible middleware
  • Content Type Detection: Automatic detection and filtering based on Content-Type headers
  • Compression Support: Built-in support for gzip, br (brotli) and deflate encodings
  • Error Handling: Comprehensive error handling for malformed data, size limits, and unsupported formats
  • Verification: Optional verification hooks for custom validation of request bodies

Capabilities

JSON Parser

Parses 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;

JSON Parser

URL-encoded Parser

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;

URL-encoded Parser

Raw Parser

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;

Raw Parser

Text Parser

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;

Text Parser

Deprecated Functions

bodyParser()

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 bodies
  • bodyParser.urlencoded() for form data
  • bodyParser.raw() for raw buffers
  • bodyParser.text() for text content

Types

// 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;
}

Error Handling

All parsers can throw errors with specific status codes:

  • 400 Bad Request: Parse errors, malformed data
  • 403 Forbidden: Verification failure
  • 413 Payload Too Large: Body size limit exceeded, too many parameters
  • 415 Unsupported Media Type: Unsupported charset or content encoding
app.use(bodyParser.json());

app.use((err, req, res, next) => {
  if (err.status === 400) {
    return res.status(400).send('Bad JSON');
  }
  next(err);
});