express-body-formatter

1.0.1 • Public • Published

express-body-formatter

An express middleware that formats the request body to a particular unified style

Installation

Using npm

npm install --save express-body-formatter

Using yarn

yarn add express-body-formatter

Usage

const express = require("express");
const bodyParser = require("body-parser");
const bodyFormatter = require("express-body-formatter");
const app = express();

app.use(bodyParser.json());
const options = { exclude: ["name"], trim: true, transform: "lowerCase" }
app.use(bodyFormatter(options));
app.use("/", function(req, res) {
  res.json(req.body);
});

app.listen(3000);

if a json object is sent to the server, for example:

{"name": "John Doe ", "status": " I love programmING "}

your object will become:

{name: "John Doe ", "status": "i love programming"}

name will remain the same because it was inluded in the exclude array option but status will be formatted accordingly

Options

all options are optional, to use the default options, you can either send an empty object as an argument or not send an arguement at all

options type description
exclude array an array of object keys to exclude from formatting, useful when exempting data that needs to remain it was sent
trim boolean if set to true, it removes any whitespace before and after a given value, default value is true
transform string value of "lowerCase" converts all string to lowercase characters,"upperCase" converts all string to upperCase characters, "capitalize" makes the first letter of each word a capital letter. default value is lowerCase

Package Sidebar

Install

npm i express-body-formatter

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

20.4 kB

Total Files

6

Last publish

Collaborators

  • klexzi