HTTP request body-parsing for modern Node.js.
π This project is a fork of tinyhttp/milliparsec.
- β© built with
async
/ `await - π support for
- JSON
- urlencoded
- text
- multipart/form-data
- π₯ no dependencies
- β¨ otterhttp support
- β‘ 30% faster than body-parser
# pnpm
pnpm i @otterhttp/parsec
# yarn
yarn add @otterhttp/parsec
# npm
npm i @otterhttp/parsec
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))
})
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`))
Minimal body parsing without any formatting.
Converts request body to string.
Parses request body using new URLSearchParams
.
Parses request body using JSON.parse
.
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"
The parsec is a unit of length used to measure large distances to astronomical objects outside the Solar System.