1.1.2 • Published 2 years ago

@becodebg/chocomen v1.1.2

Weekly downloads
28
License
MIT
Repository
bitbucket
Last release
2 years ago

chocomen

Stolen from bodymen with some customization

Chocoman

chocomen works similarly to Querymen and has almost the same functionality, expect it formats, validates and parses request body instead of querystrings. Refer to Querymen's readme to find out more.

Prerequisites

You must use a request body parser like express body-parser and set it up before using chocomen:

import express from 'express'
import bodyParser from 'body-parser'

const app = express()

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

Install

npm install --save chocomen

Usage

chocomen allows you to define a schema to control the fields sent through the request body.

import chocomen, { errorHandler } from "chocomen"

app.post('/posts', chocomen.middleware({
  title: {
    type: String,
    required: true,
    trim: true,
    minlength: 3
  },
  content: {
    type: String,
    required: true,
    minlength: 32
  },
  tags: [String]
}), (req, res) => {
  console.log(req.chocomen.body) // will contain the parsed body
})

app.use(errorHandler()) // will send standard error messages, similar to Querymen