1.1.3 • Published 10 months ago

jai-body-parser v1.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Jai Body Parser

Simple and fast Node.js module for parsing Http request body. Part of Jai.js ecosystem. Built without any third part dependency. Jai body parser is a middleware for node.js that parses incoming request body in a middleware before your application’s request handlers are called. Allowed content-types 'application/x-www-form-urlencoded', 'text/plain', 'application/json', 'application/javascript','application/xml'.


Twitter Follow Linkedin: Harpal Singh

GitHub followers

Features

  • Easy Setup
  • Config the request body size
  • Config specific request methods to implement/use
  • Config request header content-types to use
  • option to save raw body or payload
  • Can be used with any framework

Installation

Install my-project with npm

  npm install jai-body-parser

Usage / Examples

// JAI SERVER

const jaiServer = require('jai-server');
const jaiBodyParser = require('jai-body-parser');

const app = jaiServer();
const port = 1111;
app.use(jaiBodyParser(/* options */));
app.post('*', (req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ body: req.body }));
});


app.listen(port, () => {
  console.log(`Server listening on http://localhost:${port}/ ...`);
});


// Express

const express = require('express');
const jaiBodyParser = require('jai-body-parser');

const app = express();
const port = 1111;
app.use(jaiBodyParser(/* options */));
app.post('*', (req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ body: req.body }));
});


app.listen(port, () => {
  console.log(`Server listening on http://localhost:${port}/ ...`);
});


//  OR Http

const http = require('http');
const jaiBodyParser = require('jai-body-parser');

const server = http.createServer(async (req, res) => {
  jaiBodyParser(/* options */)(req, res, (err) => {
    if (err) {
      res.writeHead(200, { 'Content-Type': 'application/json' });
      return res.end(JSON.stringify({ Error: err.stack }));
    }
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ body: req.body }));
  });
});

server.listen(1111, () => {
  console.log('Server listening on http://localhost:1111/ ...');
});

API Reference

Options

/ { limit: 100, // in kb shouldSaveRawBody: false, allowedMethods: eligibleMethods, allowedContentTypes: contentTypes, parseNumbers: true } /

ParameterTypeDescription
limitintegersize in kb, default: 100 (100kb)
shouldSaveRawBodybooleanshould save raw body, default: false. useful when authenticating webhook responses
allowedMethodsarrayarray of allowed http methods, default: 'post', 'put', 'patch'
allowedContentTypesarrayarray of allowed http methods, default: ['application/x-www-form-urlencoded' 'text/plain',
'application/json', 'application/javascript', 'application/xml']
parseNumbersbooleanparse numbers from body text, default: true,