koa-formidable
Version:
Formidable middleware for Koa
63 lines (41 loc) • 1.68 kB
Markdown
# koa-formidable
[Formidable](https://github.com/felixge/node-formidable) middleware for Koa
[![NPM][npm]](https://npmjs.org/package/koa-formidable)
[![Dependency Status][dependencies]](https://david-dm.org/rkusa/koa-formidable)
**Breaking Change in 1.0.0:** both `body` and `files` are now added to Koa's `.request` instead of modifying the http request (`.req`) directly.
## API
`var formidable = require('koa-formidable')`
### formidable(opts)
Returns the formidable middleware that parses the incoming request and adds the `.request.body` and `.request.files` to the context.
**Arguments:**
* **opts** - the options that get passed to the [`Formidable.IncomingForm`](https://github.com/felixge/node-formidable#formidableincomingform) (you could also provide an instance of `IncomingForm` directly)
**Example:**
```js
var formidable = require('koa-formidable')
app.use(formidable())
```
### formidable.parse(opts, ctx)
Parse the incoming request manually.
**Arguments:**
* **opts** - the options that get passed to the [`Formidable.IncomingForm`](https://github.com/felixge/node-formidable#formidableincomingform) (you could also provide an instance of `IncomingForm` directly)
* **ctx** - the Koa context
**Example:**
```js
var formidable = require('koa-formidable')
app.use(function*(next) {
var form = yield formidable.parse(this)
...
yield next
})
```
### Using formidable Events
**Example:**
```js
var form = new require('formidable').IncomingForm()
form.on('progress', function(bytesReceived, bytesExpected) {
console.log(bytesReceived, bytesExpected)
})
var result = yield formidable.parse(form, this)
```
## License
[MIT](LICENSE)