body-bson
express/koa middleware to parse bson body.
Example
For koa:
var Koa = require('koa');
var bodyBson = require('body-bson');
var app = new Koa();
app.use(bodyBson());
app.use(async ctx => {
var body = ctx.request.body;
});
For express
var express = require('express')
var bodyBson = require('body-bson')
var app = express()
app.use(bodyBson.json())
app.use(function (req, res) {
var body= req.body;
})
API
bodyBson
var bodyBson = require('body-bson')
The bodyBson function is a factory to create middleware.
app.use(bodyBson(option))
The middleware will populate the request.rawBody
property with the parsed body when the Content-Type request header matches the type option(default to
application/bson
), nothing would be modified if the Content-Type was not matched, or an error occurred.
bodyBson.bson
The default bson instance used by bodyBson. It's exported for convenience.
options
bson
Controls the bson instance to deserialize the body.
One method is required for the instance: bson.deserialize(buffer)
.
Defaults to instance created by bson@1.1.0
.
limit
Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a
string, the value is passed to the bytes
library for parsing. Defaults to '10mb'.
type
Controls the matching Content-Type headers, can pass a string or an array of strings. Defaults to 'application/bson'
.
rawBody
Controls whether to generate request.rawBody
to represent raw buffer received. request.rawBody
will only be generated if request.rawBody
should be generated. Defaults to false
.