@ukab/jbody
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

Json Body Parser

The library is compitable with both ESM and CommonJS. You can use this package with any framework or even with NodeJS native http module.

Usage

with native http module

const { createServer } = require('http');

const jbody = require('@ukab/jbody');

// // with config
// const jsonParser = jbody({
//   exclude: ['/contact'] // optional
//   excludeRegex: [new RegExp('/users/.*')] // optional
//   sizeLimitInBytes: 1024 // optional
// });

// without config

const jsonParser = jbody();

createServer(async (req, res) => {
  const url = new URL(req.url);

  const ctx = { req, res, url: url.pathname };

  await jsonParser(ctx);

  res.end(JSON.stringify({
    data: ctx.body,
  }));

}).listen(3000);

with koa

app.use(async (ctx, next) => {
   await jsonParser(ctx);
   await next();
});

with express

app.use((req, res, next) => {
  const ctx = { req, res, url: req.url };

   jsonParser(ctx)
    .then(() => {
      next();
    }).catch(next);
});

Package Sidebar

Install

npm i @ukab/jbody

Weekly Downloads

2

Version

0.2.0

License

UNLICENSED

Unpacked Size

10 kB

Total Files

14

Last publish

Collaborators

  • bitnbytes