Giter VIP home page Giter VIP logo

jai-body-parser's Introduction

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 } */

Parameter Type Description
limit integer size in kb, default: 100 (100kb)
shouldSaveRawBody boolean should save raw body, default: false. useful when authenticating webhook responses
allowedMethods array array of allowed http methods, default: ['post', 'put', 'patch']
allowedContentTypes array array of allowed http methods, default: ['application/x-www-form-urlencoded' 'text/plain',
'application/json', 'application/javascript', 'application/xml']
parseNumbers boolean parse numbers from body text, default: true,

jai-body-parser's People

Contributors

hsk11 avatar

Stargazers

 avatar  avatar

Forkers

hsk11

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.