Skip to content

OtterJS/parsec

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

@otterhttp/parsec

HTTP request body-parsing for modern Node.js.

πŸ“Œ This project is a fork of tinyhttp/milliparsec.

npm GitHub Workflow Status Coverage

Features

  • ⏩ built with async / `await
  • πŸ›  support for
    • JSON
    • urlencoded
    • text
    • multipart/form-data
  • πŸ”₯ no dependencies
  • ✨ otterhttp support
  • ⚑ 30% faster than body-parser

Install

# pnpm
pnpm i @otterhttp/parsec

# yarn
yarn add @otterhttp/parsec

# npm
npm i @otterhttp/parsec

Usage

Basic example

Use a middleware inside a server:

import { createServer } from 'node:http'
import { json } from '@otterhttp/parsec'

const parseJsonBody = json()

const server = createServer(async (req: HasBody, res) => {
  await parseJsonBody(req, res, (err) => void console.log(err))

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

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

Web frameworks integration

otterhttp

import { App } from '@otterhttp/app'
import { urlencoded } from '@otterhttp/parsec'

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 makeCustom(
        req,
        (d) => d.toUpperCase(),
        (err) => {
        }
)
res.end(req.body) // "THIS TEXT MUST BE UPPERCASED"

What does "parsec" mean?

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

About

🌌 HTTP request body-parsing for modern Node.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 99.1%
  • JavaScript 0.9%