Skip to content

dan-dr/co-better-body

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

co-better-body

Parse request bodies with generators inspired by Raynos/body.

Better? WAT?

co-body has outdated dependencies. plus you can't pass options to qs meaning you're stuck with maximum depth of 5 for parsing forms using object and array notations in field names. Since it seems all but abandoned, and there's quite a few body parsing packages that depend on it, I made some quick changes only updating the qs and raw-body versions and added passing options to qs.

Installation

$ npm install co-body

Options

Available via raw-body:

  • limit number or string representing the request size limit (1mb for json and 56kb for form-urlencoded)

Available via qs:

  • depth number representing the parsed object maximum hierarchy depth, default 5
  • delimiter string or regex representing the query string delimeter, default &
  • arrayLimit nubmer representing the maximum array size or -1 to disable arrays, default 20

Example

// application/json
var body = yield parse.json(req);

// explicit limit
var body = yield parse.json(req, { limit: '10kb' });

// qs depth
var body = yield parse.json(req,  {
    limit: '10kb',
    qs: {
     depth: 10
    }
});

// application/x-www-form-urlencoded
var body = yield parse.form(req);

// either
var body = yield parse(req);

Koa

This lib also supports ctx.req in Koa (or other libraries), so that you may simply use this instead of this.req.

// application/json
var body = yield parse.json(this);

// application/x-www-form-urlencoded
var body = yield parse.form(this);

// either
var body = yield parse(this);

License

MIT

About

Parse request bodies with co

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 97.2%
  • Makefile 2.8%