A full-featured koa body parser middleware. Supports multipart, urlencoded, and json request bodies. Provides the same functionality as Express's bodyParser - multer.
It's generally better to only parse the body as needed, if using a router that supports middleware composition, we can inject it only for certain routes.
constKoa=require('koa');constapp=newKoa();constrouter=require('koa-router')();constkoaBody=require('koa-body');router.post('/users',koaBody(),(ctx)=>{console.log(ctx.request.body);// => POST bodyctx.body=JSON.stringify(ctx.request.body);});app.use(router.routes());app.listen(3000);console.log('curl -i http://localhost:3000/users -d "name=test"');
Usage with unsupported text body type
For unsupported text body type, for example, text/xml, you can use the unparsed request body at ctx.request.body. For the text content type, the includeUnparsed setting is not required.
text{Boolean} Parse text bodies, such as XML, default true
json{Boolean} Parse JSON bodies, default true
jsonStrict{Boolean} Toggles co-body strict mode; if set to true - only parses arrays or objects, default true
includeUnparsed{Boolean} Toggles co-body returnRawBody option; if set to true, for form encoded and JSON requests the raw, unparsed request body will be attached to ctx.request.body using a Symbol (see details), default false
formidable{Object} Options to pass to the formidable multipart parser
onError{Function} Custom error handle, if throw an error, you can customize the response - onError(error, context), default will throw
GET, HEAD, and DELETE requests have no defined semantics for the request body, but this doesn't mean they may not be valid in certain use cases.
koa-body is strict by default, parsing only POST, PUT, and PATCH requests
File Support
Uploaded files are accessible via ctx.request.files.
A note about unparsed request bodies
Some applications require crytopgraphic verification of request bodies, for example webhooks from slack or stripe. The unparsed body can be accessed if includeUnparsed is true in koa-body's options. When enabled, import the symbol for accessing the request body from unparsed = require('koa-body/unparsed.js'), or define your own accessor using unparsed = Symbol.for('unparsedBody'). Then the unparsed body is available using ctx.request.body[unparsed].
maxFields{Integer} Limits the number of fields that the querystring parser will decode, default 1000
maxFieldsSize{Integer} Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted, default 2mb (2 * 1024 * 1024)
uploadDir{String} Sets the directory for placing file uploads in, default os.tmpDir()
keepExtensions{Boolean} Files written to uploadDir will include the extensions of the original files, default false
hashAlgorithm{String} If you want checksums calculated for incoming files, set this to either 'sha1' or 'md5', default false
multiples{Boolean} Multiple file uploads or no, default true
onFileBegin{Function} Special callback on file begin. The function is executed directly by formidable. It can be used to rename files before saving them to disk. See the docs
Changelog
Please see the Changelog for a summary of changes.
请发表评论