Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
body-parsec
Advanced tools
Readme
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.
async
/ await
⏩# pnpm
pnpm i body-parsec
# yarn
yarn add body-parsec
# npm
npm i body-parsec
Use a middleware inside a server:
import { createServer } = from 'http'
import { json } from 'body-parsec'
createServer(async (req, res) => {
const parsedData = await json()(req)
console.log(parsedData) // { 'hello': 'world' }
res.setHeader('Content-Type', 'application/json')
res.end(req.body.hello)
}).listen(80)
Then try to make a request to our server:
curl -d '{ "hello": "world" }' localhost
After sending a request, it should output world
.
import { App } from '@tinyhttp/app'
import { urlencoded } from 'body-parsec'
const app = new App()
app.use(urlencoded()).post('/', (req, res) => {
res.send(req.body)
})
app.listen(3000, () => console.log(`Started on http://localhost:3000`))
import Express from 'express'
import { urlencoded } from 'body-parsec'
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`))
import Koa from 'koa'
import { json, CtxWithBody } from 'body-parsec/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`))
parsec.raw(req)
Minimal body parsing without any urlencodedatting (even without converting to string):
// Request: curl -d "Hello World"
await parsec.raw()(req)
res.end(req.body) // "Hello World"
parsec.text(req)
Converts request body to string.
// Request: curl -d "Hello World"
await parsec.text()(req)
res.end(req.body) // "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" localhost
await parsec.custom(req, (data) => data.toUpperCase())
res.end(req.body) // "THIS TEXT MUST BE UPPERCASED"
parsec.json(req)
Parses request body using JSON.parse
.
// Request: curl -d { "hello": "world" } localhost
await parsec.json()(req)
res.end(req.body.hello) // world
parsec.urlencoded(req)
Parses request body using querystring.parse
.
// Request: curl -d 'username=pro_gamer'
await parsec.urlencoded()(req)
res.end(req.body.username) // pro_gamer
The parsec (symbol: pc) is a unit of length used to measure large distances to astronomical objects outside the Solar System.
FAQs
![Twitter](https://img.shields.io/twitter/follow/v1rtl.svg?label=twitter&style=flat-square) ![Top lang](https://img.shields.io/github/languages/top/talentlessguy/parsec.svg?style=flat-square) ![Vulnerabilities](https://img.shields.io/snyk/vulnerabilities/
The npm package body-parsec receives a total of 8 weekly downloads. As such, body-parsec popularity was classified as not popular.
We found that body-parsec demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.