forked from eggjs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add body-parser example (eggjs#101)
- Loading branch information
Showing
6 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |