Skip to content

Commit 1c75fdf

Browse files
author
tunnckoCore
committed
docs(example): add section for koa-router with working ./example.js
1 parent e7f1884 commit 1c75fdf

3 files changed

Lines changed: 89 additions & 8 deletions

File tree

.verb.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,44 @@ npm i {%= name %} --save
3939
const {%= varname %} = require('{%= name %}')
4040
```
4141

42+
## Working with [koa-router][]
43+
44+
```js
45+
'use strict'
46+
47+
var app = require('koa')()
48+
var body = require('{%= name %}')
49+
var router = require('koa-router')()
50+
51+
router.post('/upload', body(), function * (next) {
52+
console.log(this.request.files)
53+
console.log(this.request.fields)
54+
55+
// there's no `.body` when `multipart`,
56+
// `urlencoded` or `json` request
57+
console.log(this.request.body)
58+
59+
// print it to the API requester
60+
this.body = JSON.stringify({
61+
fields: this.request.fields,
62+
files: this.request.files,
63+
body: this.request.body || null
64+
}, null, 2)
65+
66+
yield next
67+
})
68+
69+
app.use(router.routes())
70+
app.listen(4292)
71+
72+
var format = require('util').format
73+
var host = 'http://localhost:4292'
74+
var cmd = 'curl -i %s/upload -F "source=@%s/.editorconfig"'
75+
76+
console.log('Try it out with below CURL for `{%= name %}` repository.')
77+
console.log(format(cmd, host, __dirname))
78+
```
79+
4280
{%= apidocs('index.js') %}
4381

4482
## Options

README.md

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,44 @@ npm i koa-better-body --save
3838
const koaBetterBody = require('koa-better-body')
3939
```
4040

41+
## Working with [koa-router][]
42+
43+
```js
44+
'use strict'
45+
46+
var app = require('koa')()
47+
var body = require('koa-better-body')
48+
var router = require('koa-router')()
49+
50+
router.post('/upload', body(), function * (next) {
51+
console.log(this.request.files)
52+
console.log(this.request.fields)
53+
54+
// there's no `.body` when `multipart`,
55+
// `urlencoded` or `json` request
56+
console.log(this.request.body)
57+
58+
// print it to the API requester
59+
this.body = JSON.stringify({
60+
fields: this.request.fields,
61+
files: this.request.files,
62+
body: this.request.body || null
63+
}, null, 2)
64+
65+
yield next
66+
})
67+
68+
app.use(router.routes())
69+
app.listen(4292)
70+
71+
var format = require('util').format
72+
var host = 'http://localhost:4292'
73+
var cmd = 'curl -i %s/upload -F "source=@%s/.editorconfig"'
74+
75+
console.log('Try it out with below CURL for `koa-better-body` repository.')
76+
console.log(format(cmd, host, __dirname))
77+
```
78+
4179
### [koaBetterBody](index.js#L40)
4280
> Robust body parser for [koa][]@1, also works for `koa@2` (with deprecations). Will also work for future `koa@3` with [koa-convert][].
4381
@@ -147,10 +185,10 @@ You can pass `strict:false`, but see [IETF HTTP/1.1 Message Semantics: Section 6
147185
## Related
148186
You might also be interested in these packages:
149187
- [formidable](https://www.npmjs.com/package/formidable): A node.js module for parsing form data, especially file uploads. | [homepage](https://github.com/felixge/node-formidable "A node.js module for parsing form data, especially file uploads.")
150-
- [ip-filter](https://www.npmjs.com/package/ip-filter): Filters valid IPv4 or IPv6 against glob pattern, array, string and etc. If match[more](https://github.com/tunnckocore/ip-filter#readme) | [homepage](https://github.com/tunnckocore/ip-filter#readme "Filters valid IPv4 or IPv6 against glob pattern, array, string and etc. If match returns passed `ip`, otherwise null is returned. Have no strict mode to check no IP values.")
188+
- [ip-filter](https://www.npmjs.com/package/ip-filter): Validates valid IPs (IPv4 and IPv6) using [micromatch][] - glob patterns, RegExp, string or[more](https://github.com/tunnckocore/ip-filter#readme) | [homepage](https://github.com/tunnckocore/ip-filter#readme "Validates valid IPs (IPv4 and IPv6) using [micromatch][] - glob patterns, RegExp, string or array of globs. If match returns the IP, otherwise null.")
151189
- [koa-body-parsers](https://www.npmjs.com/package/koa-body-parsers): collection of koa body parsers | [homepage](https://github.com/koajs/body-parsers#readme "collection of koa body parsers")
152190
- [koa-bodyparser](https://www.npmjs.com/package/koa-bodyparser): a body parser for koa | [homepage](https://github.com/koajs/body-parser "a body parser for koa")
153-
- [koa-ip-filter](https://www.npmjs.com/package/koa-ip-filter): koa middleware to filter request IPs or custom ID with glob patterns, array, string… [more](https://github.com/tunnckocore/koa-ip-filter#readme) | [homepage](https://github.com/tunnckocore/koa-ip-filter#readme "koa middleware to filter request IPs or custom ID with glob patterns, array, string, regexp or matcher function. Support custom `403 Forbidden` message and custom ID.")
191+
- [koa-ip-filter](https://www.npmjs.com/package/koa-ip-filter): Middleware for [koa][] that filters IPs against glob patterns, RegExp, string or array of[more](https://github.com/tunnckocore/koa-ip-filter#readme) | [homepage](https://github.com/tunnckocore/koa-ip-filter#readme "Middleware for [koa][] that filters IPs against glob patterns, RegExp, string or array of globs. Support custom `403 Forbidden` message and custom ID.")
154192
- [koa](https://www.npmjs.com/package/koa): Koa web app framework | [homepage](https://github.com/koajs/koa#readme "Koa web app framework")
155193
- [koala](https://www.npmjs.com/package/koala): Koa Framework Suite | [homepage](https://github.com/koajs/koala#readme "Koa Framework Suite")
156194

@@ -182,8 +220,11 @@ It will help a lot, thanks in advance! :yum:
182220
[formidable]: https://github.com/felixge/node-formidable
183221
[koa-body-parsers]: https://github.com/koajs/body-parsers
184222
[koa-convert]: https://github.com/gyson/koa-convert
223+
[koa-router]: https://github.com/alexmingoia/koa-router
185224
[koa]: https://github.com/koajs/koa
186225
[lazy-cache]: https://github.com/jonschlinkert/lazy-cache
226+
[micromatch]: https://github.com/jonschlinkert/micromatch
227+
[qs]: https://github.com/ljharb/qs
187228
[raw-body]: https://github.com/stream-utils/raw-body
188229

189230
[npmjs-url]: https://www.npmjs.com/package/koa-better-body
@@ -231,4 +272,3 @@ It will help a lot, thanks in advance! :yum:
231272
[new-message-url]: https://github.com/tunnckoCore/ama
232273
[new-message-img]: https://img.shields.io/badge/ask%20me-anything-green.svg
233274

234-
[qs]: https://github.com/ljharb/qs

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,16 @@
103103
]
104104
},
105105
"reflinks": [
106+
"bytes",
107+
"formidable",
106108
"koa",
107-
"koa-convert",
108109
"koa-body-parsers",
110+
"koa-convert",
111+
"lazy-cache",
109112
"raw-body",
110-
"bytes",
111-
"formidable",
112-
"lazy-cache"
113+
"koa-router",
114+
"micromatch",
115+
"qs"
113116
],
114117
"lint": {
115118
"reflinks": true
@@ -120,4 +123,4 @@
120123
"path": "./node_modules/cz-conventional-changelog"
121124
}
122125
}
123-
}
126+
}

0 commit comments

Comments
 (0)