Skip to content

Simple and fast Node.js module for parsing Http request body. Part of Jai.js ecosystem. Built without any third part dependency.

License

Notifications You must be signed in to change notification settings

jaijs/jai-body-parser

Repository files navigation

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,

About

Simple and fast Node.js module for parsing Http request body. Part of Jai.js ecosystem. Built without any third part dependency.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published