You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+43-3Lines changed: 43 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,6 +38,44 @@ npm i koa-better-body --save
38
38
constkoaBetterBody=require('koa-better-body')
39
39
```
40
40
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
+
41
79
### [koaBetterBody](index.js#L40)
42
80
> Robust body parser for [koa][]@1, also works for `koa@2` (with deprecations). Will also work for future `koa@3` with [koa-convert][].
43
81
@@ -147,10 +185,10 @@ You can pass `strict:false`, but see [IETF HTTP/1.1 Message Semantics: Section 6
147
185
## Related
148
186
You might also be interested in these packages:
149
187
-[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.")
151
189
-[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")
152
190
-[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, stringor array of globs. Support custom `403 Forbidden` message and custom ID.")
154
192
-[koa](https://www.npmjs.com/package/koa): Koa web app framework | [homepage](https://github.com/koajs/koa#readme"Koa web app framework")
155
193
-[koala](https://www.npmjs.com/package/koala): Koa Framework Suite | [homepage](https://github.com/koajs/koala#readme"Koa Framework Suite")
156
194
@@ -182,8 +220,11 @@ It will help a lot, thanks in advance! :yum:
0 commit comments