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'.
- 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
Install my-project with npm
npm install jai-body-parser
// 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/ ...');
});
/* { 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, |