Skip to content

Commit df61e66

Browse files
authored
feat: add body-parser example (#101)
1 parent f4afa7b commit df61e66

File tree

6 files changed

+203
-0
lines changed

6 files changed

+203
-0
lines changed

bodyParser/REAME.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
## bodyParser
2+
3+
built-in [bodyparser](https://github.com/koajs/bodyparser) help us to parse `POST` body.
4+
5+
by default, only accepet `form` and `json`, access by `ctx.request.body`.
6+
7+
## Custom Type
8+
9+
Treat `application/custom-json` as `JSON` type then auto parse:
10+
11+
```js
12+
// config/config.default.js
13+
exports.bodyParser = {
14+
extendTypes: {
15+
json: 'application/custom-json',
16+
},
17+
};
18+
```
19+
20+
## XML
21+
22+
1. add `text` to `enableTypes`.
23+
2. treat `xml` as `text`, so we could access it by `ctx.request.body`.
24+
3. then parse it at `Controller` as your wish.
25+
26+
```js
27+
// config/config.default.js
28+
exports.bodyParser = {
29+
enableTypes: [ 'json', 'form', 'text' ],
30+
extendTypes: {
31+
text: [ 'application/xml' ],
32+
},
33+
};
34+
```
35+
36+
```js
37+
// app/controller/home.js
38+
const { xml2js } = require('xml-js');
39+
40+
class HomeController extends Controller {
41+
async xml() {
42+
const { ctx } = this;
43+
44+
const xmlContent = xml2js(ctx.request.body);
45+
const body = xmlContent.elements[0].attributes;
46+
47+
ctx.body = {
48+
type: ctx.get('content-type'),
49+
body,
50+
};
51+
}
52+
}
53+
54+
module.exports = HomeController;
55+
```
56+
57+
## Size
58+
59+
By default, body size is limit to `100kb`, you could custom it by:
60+
61+
```js
62+
// config/config.default.js
63+
module.exports = {
64+
bodyParser: {
65+
jsonLimit: '1mb',
66+
formLimit: '1mb',
67+
},
68+
};
69+
```

bodyParser/app/controller/home.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const Controller = require('egg').Controller;
4+
const { xml2js } = require('xml-js');
5+
6+
class HomeController extends Controller {
7+
async body() {
8+
const { ctx } = this;
9+
10+
ctx.body = {
11+
type: ctx.get('content-type'),
12+
body: ctx.request.body,
13+
};
14+
}
15+
16+
async xml() {
17+
const { ctx } = this;
18+
19+
const xmlContent = xml2js(ctx.request.body);
20+
const body = xmlContent.elements[0].attributes;
21+
22+
ctx.body = {
23+
type: ctx.get('content-type'),
24+
body,
25+
};
26+
}
27+
}
28+
29+
module.exports = HomeController;

bodyParser/app/router.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
const { router, controller } = app;
5+
6+
router.post('/api/body', controller.home.body);
7+
router.post('/api/xml', controller.home.xml);
8+
};

bodyParser/config/config.default.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
exports.keys = '123456';
4+
5+
exports.bodyParser = {
6+
enableTypes: [ 'json', 'form', 'text' ],
7+
extendTypes: {
8+
json: 'application/custom-json',
9+
text: [ 'application/xml' ],
10+
},
11+
};

bodyParser/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "bodyParser",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"egg": "^2",
6+
"xml-js": "^1.6.9"
7+
},
8+
"devDependencies": {
9+
"egg-bin": "^4.3.5",
10+
"egg-mock": "^3.13.1"
11+
},
12+
"scripts": {
13+
"dev": "egg-bin dev",
14+
"test": "egg-bin test",
15+
"cov": "egg-bin cov"
16+
},
17+
"private": true
18+
}

bodyParser/test/home.test.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict';
2+
3+
const { app } = require('egg-mock/bootstrap');
4+
5+
describe('test/app/controller/home.test.js', () => {
6+
7+
beforeEach(() => app.mockCsrf());
8+
9+
it('should parse form', () => {
10+
return app.httpRequest()
11+
.post('/api/body')
12+
.type('form')
13+
.send({ name: 'TZ' })
14+
.expect({
15+
type: 'application/x-www-form-urlencoded',
16+
body: { name: 'TZ' },
17+
})
18+
.expect(200);
19+
});
20+
21+
it('should parse json', () => {
22+
return app.httpRequest()
23+
.post('/api/body')
24+
.type('json')
25+
.send({ name: 'TZ' })
26+
.expect({
27+
type: 'application/json',
28+
body: { name: 'TZ' },
29+
})
30+
.expect(200);
31+
});
32+
33+
it('should accept raw', () => {
34+
return app.httpRequest()
35+
.post('/api/body')
36+
.type('text')
37+
.send('this is a raw')
38+
.expect({
39+
type: 'text/plain',
40+
body: 'this is a raw',
41+
})
42+
.expect(200);
43+
});
44+
45+
it('should accept xml', () => {
46+
return app.httpRequest()
47+
.post('/api/xml')
48+
.type('xml')
49+
.send('<User name="TZ"></User>')
50+
.expect({
51+
type: 'application/xml',
52+
body: { name: 'TZ' },
53+
})
54+
.expect(200);
55+
});
56+
57+
it('should accept custom', () => {
58+
return app.httpRequest()
59+
.post('/api/body')
60+
.type('application/custom-json')
61+
.send(JSON.stringify({ name: 'TZ' }))
62+
.expect({
63+
type: 'application/custom-json',
64+
body: { name: 'TZ' },
65+
})
66+
.expect(200);
67+
});
68+
});

0 commit comments

Comments
 (0)