@prexjs/body-parser
v1.0.0
Published
Node.js body parsing middleware for JSON, URL-encoded, raw buffer, and text HTTP requests.
Readme
@prexjs/body-parser
Node.js body parsing middleware for JSON, URL-encoded, raw buffer, and text HTTP requests.
@prexjs/body-parser parses incoming request bodies in standard HTTP middleware pipelines before handlers receive them, populating req.body with parsed representations. It provides dedicated parsers for JSON, form URL-encoded data, raw binary buffers, and plain text.
Features
- JSON Parser (
json): Parses JSON payloads with customizable revivers, strict typing mode, and size limits. - URL-Encoded Parser (
urlencoded): Parses UTF-8 URL-encoded form data with customizable depth and parameter limits. - Raw Body Parser (
raw): Parses incoming payloads into a binaryBufferwith strict Content-Type checks. - Text Parser (
text): Parses request streams into string bodies with customizable character set decoders. - Stream Inflation & Limit Control: Automatically uncompresses gzip/deflate streams and enforces byte limits.
- Standard Middleware Contract: Integrates seamlessly with Prex, Connect, and standard Node.js HTTP servers.
Installation
Install using your preferred package manager:
# npm
npm install @prexjs/body-parser
# pnpm
pnpm add @prexjs/body-parser
# Yarn
yarn add @prexjs/body-parser
# Bun
bun add @prexjs/body-parserQuick start
import http from 'node:http';
import bodyParser, { json, urlencoded } from '@prexjs/body-parser';
// Create JSON middleware
const parseJson = json({ limit: '1mb' });
const parseUrlencoded = urlencoded({ extended: true });
const server = http.createServer((req: any, res) => {
parseJson(req, res, (err) => {
if (err) {
res.statusCode = 400;
res.end('Bad Request: Invalid JSON');
return;
}
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ received: req.body }));
});
});
server.listen(3000);API
json(options?)
Returns middleware that only parses JSON and only looks at requests where the Content-Type header matches the type option.
limit: Maximum request body size (default:'100kb').strict: Whentrue, only accepts arrays and objects (default:true).reviver: Reviver function passed toJSON.parse.
urlencoded(options?)
Returns middleware that parses URL-encoded bodies (like HTML forms).
extended: Parse with advanced query-string parser whentrue(default:true).limit: Maximum request body size (default:'100kb').parameterLimit: Maximum number of parameters allowed (default:1000).
raw(options?)
Returns middleware that parses all bodies as a Buffer.
limit: Maximum size (default:'100kb').type: Content-Type to match (default:'application/octet-stream').
text(options?)
Returns middleware that parses all bodies as a string.
limit: Maximum size (default:'100kb').defaultCharset: Default character encoding if unspecified in headers (default:'utf-8').
Compatibility
- Node.js >= 18.0.0
- Supports both ESM and CommonJS
- TypeScript 5.0+
Related Packages
@prexjs/raw-body: Core stream reader and byte limiter.@prexjs/http-errors: Standardized HTTP error generator.@prexjs/type-is: Content-Type matcher.
