Popularity
0.9
Growing
Activity
0.0
-
33
1
0

Description

Modern asynchronous body parser for Node.js.

It puts all the data into req.body so you don't have to create a separate array for it.

Supports Node.js 13+ ESM, CJS.

Integrates with Express, Koa and native `http` module

Monthly Downloads: 0
Programming language: TypeScript
License: MIT License
Tags: JSON     Parsing     Node.Js     Forms     Body Parsing     Request Parsing    

parsec ๐ŸŒŒ alternatives and similar modules

Based on the "Parsing" category.
Alternatively, view parsec ๐ŸŒŒ alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of parsec ๐ŸŒŒ or a related project?

Add another 'Parsing' Module

README

milliparsec ๐ŸŒŒ

Photo by NASA published on Unsplash

Top lang Vulnerabilities Version Last commit Minified size Codecov

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

Features

  • ๐Ÿš€ works with Node 13+ ESM and CommonJS
  • โฉ built with async / await
  • ๐Ÿ›  JSON / raw / urlencoded / text data support
  • ๐Ÿ“ฆ tiny package size (800B)
  • ๐Ÿ”ฅ no dependencies
  • tinyhttp, Koa and Express support

Installation

# 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'

createServer(async (req, res) => {
  const parsedData = await json()(req, res, (err) => {
    if (err) {
      res.writeHead(500)
      res.end('oops')
    }
  })
  console.log(parsedData) // { 'hello': 'world' }
  res.setHeader('Content-Type', 'application/json')
  res.end(req.body.hello) // 'world'
}).listen(80)

Then try to make a request to our server:

curl -d '{ "hello": "world" }' localhost

After sending a request, it should output world.

Web frameworks integration

tinyhttp โšก

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

const app = new App()

app.use(urlencoded()).post('/', (req, res) => {
  res.send(req.body)
})

app.listen(3000, () => console.log(`Started on http://localhost:3000`))

Express

import Express from 'express'
import { urlencoded } from 'milliparsec'

const app = Express()

app.use(urlencoded())

app.get('/', (req, res) => {
  res.send(`
  <form method="POST" action="/" enctype="application/x-www-form-urlencoded">
  <input name="name" />
  </form>
  `)
})

app.post('/', (req, res) => {
  res.send(`Hello ${req.body.name}!`)
})

app.listen(3000, () => console.log(`Running on http://localhost:3000`))

Koa

import Koa from 'koa'
import { json, CtxWithBody } from 'milliparsec/koa'

const app = new Koa()

app.use(json())

app.use((ctx: CtxWithBody) => {
  if (ctx.method === 'POST') {
    ctx.type = 'application/json'
    ctx.body = ctx.req.body
  }
})

app.listen(3000, () => console.log(`Running on http://localhost:3000`))

API

parsec.raw(req, res, cb)

Minimal body parsing without any formatting (even without converting to string):

// Request: curl -d "Hello World"
await parsec.raw()(req, res, (err) => {})
res.end(req.body) // "Hello World"

parsec.text(req, res, cb)

Converts request body to string.

// Request: curl -d "Hello World"
await parsec.text()(req, res, (err) => {})
res.end(req.body) // "Hello World"

parsec.urlencoded(req, res, cb)

Parses request body using querystring.parse.

// Request: curl -d 'username=pro_gamer'
await parsec.urlencoded()(req, res, (err) => {})
res.end(req.body.username) // pro_gamer

parsec.json(req, res, cb)

Parses request body using JSON.parse.

// Request: curl -d { "hello": "world" } localhost
await parsec.json()(req, res, (err) => {})
res.end(req.body.hello) // world

parsec.custom(fn)(req, res, cb)

You can use parsec as a a handler for IncomingMessage with a custom formatter.

Here we make a request body upper case:

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

What is "parsec"?

The parsec (symbol: pc) is a unit of length used to measure large distances to astronomical objects outside the Solar System.