1.0.2 • Published 6 years ago

@makebestgame/koa-body-parser v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

@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