Skip to content

Commit

Permalink
feat: add body-parser example (eggjs#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored Feb 2, 2019
1 parent f4afa7b commit df61e66
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 0 deletions.
69 changes: 69 additions & 0 deletions bodyParser/REAME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
## bodyParser

built-in [bodyparser](https://github.com/koajs/bodyparser) help us to parse `POST` body.

by default, only accepet `form` and `json`, access by `ctx.request.body`.

## Custom Type

Treat `application/custom-json` as `JSON` type then auto parse:

```js
// config/config.default.js
exports.bodyParser = {
extendTypes: {
json: 'application/custom-json',
},
};
```

## XML

1. add `text` to `enableTypes`.
2. treat `xml` as `text`, so we could access it by `ctx.request.body`.
3. then parse it at `Controller` as your wish.

```js
// config/config.default.js
exports.bodyParser = {
enableTypes: [ 'json', 'form', 'text' ],
extendTypes: {
text: [ 'application/xml' ],
},
};
```

```js
// app/controller/home.js
const { xml2js } = require('xml-js');

class HomeController extends Controller {
async xml() {
const { ctx } = this;

const xmlContent = xml2js(ctx.request.body);
const body = xmlContent.elements[0].attributes;

ctx.body = {
type: ctx.get('content-type'),
body,
};
}
}

module.exports = HomeController;
```

## Size

By default, body size is limit to `100kb`, you could custom it by:

```js
// config/config.default.js
module.exports = {
bodyParser: {
jsonLimit: '1mb',
formLimit: '1mb',
},
};
```
29 changes: 29 additions & 0 deletions bodyParser/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const Controller = require('egg').Controller;
const { xml2js } = require('xml-js');

class HomeController extends Controller {
async body() {
const { ctx } = this;

ctx.body = {
type: ctx.get('content-type'),
body: ctx.request.body,
};
}

async xml() {
const { ctx } = this;

const xmlContent = xml2js(ctx.request.body);
const body = xmlContent.elements[0].attributes;

ctx.body = {
type: ctx.get('content-type'),
body,
};
}
}

module.exports = HomeController;
8 changes: 8 additions & 0 deletions bodyParser/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = app => {
const { router, controller } = app;

router.post('/api/body', controller.home.body);
router.post('/api/xml', controller.home.xml);
};
11 changes: 11 additions & 0 deletions bodyParser/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

exports.keys = '123456';

exports.bodyParser = {
enableTypes: [ 'json', 'form', 'text' ],
extendTypes: {
json: 'application/custom-json',
text: [ 'application/xml' ],
},
};
18 changes: 18 additions & 0 deletions bodyParser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "bodyParser",
"version": "1.0.0",
"dependencies": {
"egg": "^2",
"xml-js": "^1.6.9"
},
"devDependencies": {
"egg-bin": "^4.3.5",
"egg-mock": "^3.13.1"
},
"scripts": {
"dev": "egg-bin dev",
"test": "egg-bin test",
"cov": "egg-bin cov"
},
"private": true
}
68 changes: 68 additions & 0 deletions bodyParser/test/home.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

const { app } = require('egg-mock/bootstrap');

describe('test/app/controller/home.test.js', () => {

beforeEach(() => app.mockCsrf());

it('should parse form', () => {
return app.httpRequest()
.post('/api/body')
.type('form')
.send({ name: 'TZ' })
.expect({
type: 'application/x-www-form-urlencoded',
body: { name: 'TZ' },
})
.expect(200);
});

it('should parse json', () => {
return app.httpRequest()
.post('/api/body')
.type('json')
.send({ name: 'TZ' })
.expect({
type: 'application/json',
body: { name: 'TZ' },
})
.expect(200);
});

it('should accept raw', () => {
return app.httpRequest()
.post('/api/body')
.type('text')
.send('this is a raw')
.expect({
type: 'text/plain',
body: 'this is a raw',
})
.expect(200);
});

it('should accept xml', () => {
return app.httpRequest()
.post('/api/xml')
.type('xml')
.send('<User name="TZ"></User>')
.expect({
type: 'application/xml',
body: { name: 'TZ' },
})
.expect(200);
});

it('should accept custom', () => {
return app.httpRequest()
.post('/api/body')
.type('application/custom-json')
.send(JSON.stringify({ name: 'TZ' }))
.expect({
type: 'application/custom-json',
body: { name: 'TZ' },
})
.expect(200);
});
});

0 comments on commit df61e66

Please sign in to comment.