@makebestgame/koa-body-parser
v1.0.2
Published
koa-body-parser is a lib to parse json and form request body which to be used with [koa 2](https://github.com/koajs/koa) library.
Downloads
11
Readme
@makebestgame/koa-body-parser is a module to be used with koa 2 library.
Usage
const Koa = require('koa');
const bodyParser = require('../index.js');
const app = new Koa();
app.use(bodyParser({
jsonLimit: '20mb',
encoding: 'utf-8',
formLimit: '80mb',
uploadDir: '/tmp', // 直接上传去系统 /tmp 目录,避免后续维护过时上传文件
keepExtensions: true,
patchKoa: true,
}));
app.use(async (ctx, next) => {
const fields = ctx.request.body.fields;
console.log('post data', fields);
await next();
ctx.body = JSON.stringify(fields);
});
const port = 3001;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
test log
test-post-form.sh
post data { a: 'aaa-string', b: '1000', c: 'wahaha' }
test-post-json.sh
post data { a: 'aaa', b: 'bbb', int: 24 }
Installation
$ npm install @makebestgame/koa-body-parser