1. milliparsec
tiniest body parser in the universe
milliparsec
Package: milliparsec
Created by: talentlessguy
Last modified: Thu, 04 May 2023 16:22:41 GMT
Version: 2.3.0
License: MIT
Downloads: 141,475
Repository: https://github.com/talentlessguy/parsec

Install

npm install milliparsec
yarn add milliparsec





Vulnerabilities
Version Coverage Github actions Downloads


Tiniest body parser in the universe. Built for modern Node.js.

Check out deno-libs/parsec for Deno port.

Features

  • ⏩ built with async / await
  • 🛠 JSON / raw / urlencoded data support
  • 📦 tiny package size (728B)
  • 🔥 no dependencies
  • tinyhttp and Express support

Install

 # pnpm
pnpm i milliparsec

# yarn
yarn add milliparsec

# npm
npm i milliparsec

Usage

Basic example

Use a middleware inside a server:

 import { createServer } from 'http'
import { json } from 'milliparsec'

const server = createServer(async (req: ReqWithBody, res) => {
  await json()(req, res, (err) => void err && console.log(err))

  res.setHeader('Content-Type', 'application/json')

  res.end(JSON.stringify(req.body))
})

Web frameworks integration

tinyhttp

 import { App } from '@tinyhttp/app'
import { urlencoded } from 'milliparsec'

new App()
  .use(urlencoded())
  .post('/', (req, res) => void res.send(req.body))
  .listen(3000, () => console.log(`Started on http://localhost:3000`))

API

raw(req, res, cb)

Minimal body parsing without any formatting.

text(req, res, cb)

Converts request body to string.

urlencoded(req, res, cb)

Parses request body using new URLSearchParams.

json(req, res, cb)

Parses request body using JSON.parse.

custom(fn)(req, res, cb)

Custom function for parsec.

 // curl -d "this text must be uppercased" localhost
await custom(
  req,
  (d) => d.toUpperCase(),
  (err) => {}
)
res.end(req.body) // "THIS TEXT MUST BE UPPERCASED"

What is "parsec"?

The parsec is a unit of length used to measure large distances to astronomical objects outside the Solar System.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code