1.0.1 • Published 5 years ago

express-body-formatter v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

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

optionstypedescription
excludearrayan array of object keys to exclude from formatting, useful when exempting data that needs to remain it was sent
trimbooleanif set to true, it removes any whitespace before and after a given value, default value is true
transformstringvalue 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