npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

body-data

v2.0.4

Published

A lightweight, zero-dependency utility for extracting query parameters and request body data from both Node.js IncomingMessage and Web standard Request objects. Supports Node.js, Deno, Bun, Cloudflare Workers, and more. Type-safe, auto-adaptive, and suppo

Readme

📦 body-data

visitors npm version npm downloads bundle JSDocs License

A lightweight, zero-dependency utility for extracting query parameters and request body data from both Node.js IncomingMessage and Web standard Request objects. Fully supports Node.js (18+), Deno, Bun, Cloudflare Workers, and any modern JavaScript runtime. Type-safe, auto-adaptive, and supports all common Content-Type formats.

✨ Features

  • ✅ Works in Node.js (18+), Deno, Bun, Cloudflare Workers, and browsers
  • ✅ Accepts both Node.js IncomingMessage and Web standard Request objects
  • ✅ Type-safe with full TypeScript generics support
  • ✅ Extract query parameters from GET requests
  • ✅ Parse request body from POST requests
  • ✅ Supports application/json, x-www-form-urlencoded, text/plain, multipart/form-data, and others
  • ✅ Safe fallback parsing
  • ✅ Zero dependencies
  • ✅ Configurable parsing options: encoding, content-type override, raw mode, and custom error handling

📦 Installation

npm install body-data
# or
pnpm add body-data

🚀 Usage Examples

1. Using bodyData

bodyData is a high-level utility that returns both params (query) and body data.

import http from 'node:http'
import { bodyData } from 'body-data'

http.createServer(async (req, res) => {
  const data = await bodyData(req)

  res.setHeader('Content-Type', 'application/json')
  res.end(JSON.stringify(data))
}).listen(3000)

Request Example:

curl "http://localhost:3000?name=lete&age=18" \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"gender":"male"}'

Response:

{
  "params": {
    "name": "lete",
    "age": "18"
  },
  "body": {
    "gender": "male"
  }
}

2. Using getParams

Use getParams to only extract query parameters from the URL.

import http from 'node:http'
import { getParams } from 'body-data'

http.createServer((req, res) => {
  const params = getParams(req)

  res.setHeader('Content-Type', 'application/json')
  res.end(JSON.stringify({ params }))
}).listen(3000)

Request Example:

curl "http://localhost:3000?foo=bar&count=10"

Response:

{
  "params": {
    "foo": "bar",
    "count": "10"
  }
}

3. Using getBody

Use getBody to only extract the body from a POST request.

import http from 'node:http'
import { getBody } from 'body-data'

http.createServer(async (req, res) => {
  const body = await getBody(req)

  res.setHeader('Content-Type', 'application/json')
  res.end(JSON.stringify({ body }))
}).listen(3000)

Request Example:

curl "http://localhost:3000" \
  -X POST \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "username=test&password=1234"

Response:

{
  "body": {
    "username": "test",
    "password": "1234"
  }
}

📖 API Reference

bodyData(req: IncomingMessage): Promise<{ params, body }>

Returns an object with:

  • params: Query parameters (from URL)
  • body: Parsed request body

getParams(req: IncomingMessage): Record<string, any>

Parses the query string from the request URL.

getBody(req: IncomingMessage): Promise<Record<string, any>>

Parses the body of the request based on Content-Type. Supports:

  • application/json
  • application/x-www-form-urlencoded
  • text/plain
  • multipart/form-data (returns raw string)
  • Fallback: returns { raw: string }

Options (IBodyOptions):

| Option | Type | Description | | ----------------- | ---------------------- | ----------------------------------------------------------- | | raw | boolean | Return raw body string instead of parsing. Default: false | | encoding | BufferEncoding | Text encoding for reading the body. Default: 'utf-8' | | contentType | string | Force a specific Content-Type (overrides request headers) | | backContentType | string | Fallback Content-Type when none is provided | | onError | (err: Error) => void | Custom error handler for parse or stream errors |

✅ Example with Custom Options

const body = await getBody(req, {
  raw: false,
  encoding: 'utf-8',
  contentType: 'application/json',
  backContentType: 'text/plain',
  onError: err => console.error('Body parse error:', err),
})

🧪 Testing

pnpm test

📄 License

MIT License © Lete114