What is co-body?
The co-body npm package is a body parser for node.js that supports parsing JSON, form, and text bodies. It is designed to work with generators and promises, making it suitable for use with Koa and other async frameworks.
What are co-body's main functionalities?
Parse JSON body
This feature allows you to parse JSON bodies from incoming requests. The code sample demonstrates how to use co-body to parse a JSON body in a Koa application.
const coBody = require('co-body');
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx) => {
if (ctx.method === 'POST' && ctx.is('application/json')) {
const body = await coBody.json(ctx.req);
ctx.body = body;
}
});
app.listen(3000);
Parse form-urlencoded body
This feature allows you to parse form-urlencoded bodies from incoming requests. The code sample demonstrates how to use co-body to parse a form-urlencoded body in a Koa application.
const coBody = require('co-body');
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx) => {
if (ctx.method === 'POST' && ctx.is('application/x-www-form-urlencoded')) {
const body = await coBody.form(ctx.req);
ctx.body = body;
}
});
app.listen(3000);
Parse text body
This feature allows you to parse plain text bodies from incoming requests. The code sample demonstrates how to use co-body to parse a text body in a Koa application.
const coBody = require('co-body');
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx) => {
if (ctx.method === 'POST' && ctx.is('text/plain')) {
const body = await coBody.text(ctx.req);
ctx.body = body;
}
});
app.listen(3000);
Other packages similar to co-body
koa-bodyparser
koa-bodyparser is a body parser middleware for Koa that supports parsing JSON, form, and text bodies. It is similar to co-body but is designed specifically as middleware for Koa, making it easier to integrate into Koa applications.
body-parser
body-parser is a body parsing middleware for Express that supports parsing JSON, form, and text bodies. It is similar to co-body but is designed specifically for use with Express applications.
raw-body
raw-body is a lower-level body parsing library that provides a way to read the raw request body. It is more flexible than co-body but requires more manual handling of different content types.
co-body
Parse request bodies with generators inspired by Raynos/body.
Installation
$ npm install co-body
Options
limit
number or string representing the request size limit (1mb for json and 56kb for form-urlencoded)strict
when set to true
, JSON parser will only accept arrays and objects; when false
will accept anything JSON.parse
accepts. Defaults to true
. (also strict
mode will always return object).queryString
an object of options when parsing query strings and form data. See qs for more information.returnRawBody
when set to true
, the return value of co-body
will be an object with two properties: { parsed: /* parsed value */, raw: /* raw body */}
.jsonTypes
is used to determine what media type co-body will parse as json, this option is passed directly to the type-is library.formTypes
is used to determine what media type co-body will parse as form, this option is passed directly to the type-is library.textTypes
is used to determine what media type co-body will parse as text, this option is passed directly to the type-is library.
more options available via raw-body:
Example
var body = yield parse.json(req);
var body = yield parse.json(req, { limit: '10kb' });
var body = yield parse.form(req);
var body = yield parse.text(req);
var body = yield parse(req);
var body = yield parse(req, { textTypes: ['text', 'html'] });
Koa
This lib also supports ctx.req
in Koa (or other libraries),
so that you may simply use this
instead of this.req
.
var body = yield parse.json(this);
var body = yield parse.form(this);
var body = yield parse.text(this);
var body = yield parse(this);
License
MIT