parsec 🌌
Tiniest body parser ever for Node.js.
It puts all the data into req.body
so you don't have to create a separate array for it.
parsec is a part of tinyhttp ecosystem.
Features 👀
- works with Node 13+ ESM and CommonJS 🚀
- built with
async
/await
⏩ - JSON / raw / urlencoded / text data support 🛠
- tiny package size (879B) 📦
- no dependencies 🔥
- tinyhttp, Koa and Express support
Installation 🔄
# pnpm pnpm i body-parsec # yarn yarn add body-parsec # npm npm i body-parsec
Usage ⏩
Basic example 🖐
Use a middleware inside a server:
Then try to make a request to our server:
curl -d '{ "hello": "world" }' localhost
After sending a request, it should output world
.
Parsec and web frameworks 💻
tinyhttp ⚡
app.useurlencoded.post'/', app.listen3000,console.log`Started on http://localhost:3000`
Express
app.useurlencoded app.get'/', app.post'/', app.listen3000,console.log`Running on http://localhost:3000`
Koa
app.usejson app.use app.listen3000,console.log`Running on http://localhost:3000`
API 📦
parsec.raw(req)
Minimal body parsing without any urlencodedatting (even without converting to string):
// Request: curl -d "Hello World"await parsecreqres // "Hello World"
parsec.text(req)
Converts request body to string.
// Request: curl -d "Hello World"await parsectextreqres // "Hello World"
parsec.custom(req, fn)
You can use parsec
as a a handler for IncomingMessage
with a custom urlencodedatter.
Here we make a request body upper case:
// Request: curl -d "this text must be uppercased" localhostawait parsecres // "THIS TEXT MUST BE UPPERCASED"
parsec.json(req)
Parses request body using JSON.parse
.
// Request: curl -d { "hello": "world" } localhostawait parsecreqres // world
parsec.urlencoded(req)
Parses request body using querystring.parse
.
// Request: curl -d 'username=pro_gamer'await parsecreqres // pro_gamer
What is "parsec"?
The parsec (symbol: pc) is a unit of length used to measure large distances to astronomical objects outside the Solar System.