├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Markus Ast 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # koa-formidable 2 | 3 | [Formidable](https://github.com/felixge/node-formidable) middleware for Koa 4 | 5 | [![NPM][npm]](https://npmjs.org/package/koa-formidable) 6 | [![Dependency Status][dependencies]](https://david-dm.org/rkusa/koa-formidable) 7 | 8 | **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. 9 | 10 | ## API 11 | 12 | `var formidable = require('koa-formidable')` 13 | 14 | ### formidable(opts) 15 | 16 | Returns the formidable middleware that parses the incoming request and adds the `.request.body` and `.request.files` to the context. 17 | 18 | **Arguments:** 19 | 20 | * **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) 21 | 22 | **Example:** 23 | 24 | ```js 25 | var formidable = require('koa-formidable') 26 | app.use(formidable()) 27 | ``` 28 | 29 | ### formidable.parse(opts, ctx) 30 | 31 | Parse the incoming request manually. 32 | 33 | **Arguments:** 34 | 35 | * **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) 36 | * **ctx** - the Koa context 37 | 38 | **Example:** 39 | 40 | ```js 41 | var formidable = require('koa-formidable') 42 | app.use(function*(next) { 43 | var form = yield formidable.parse(this) 44 | ... 45 | yield next 46 | }) 47 | ``` 48 | 49 | ## MIT License 50 | 51 | Copyright (c) 2014 Markus Ast 52 | 53 | Permission is hereby granted, free of charge, to any person obtaining a copy of 54 | this software and associated documentation files (the "Software"), to deal in 55 | the Software without restriction, including without limitation the rights to 56 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 57 | the Software, and to permit persons to whom the Software is furnished to do so, 58 | subject to the following conditions: 59 | 60 | The above copyright notice and this permission notice shall be included in all 61 | copies or substantial portions of the Software. 62 | 63 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 64 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 65 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 66 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 67 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 68 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 69 | 70 | [npm]: http://img.shields.io/npm/v/koa-formidable.svg?style=flat 71 | [dependencies]: http://img.shields.io/david/rkusa/koa-formidable.svg?style=flat 72 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var formidable = require('formidable') 4 | 5 | var middleware = module.exports = function(opts) { 6 | return function *formidable(next) { 7 | var res = yield middleware.parse(opts, this) 8 | this.request.body = res.fields 9 | this.request.files = res.files 10 | yield next 11 | } 12 | } 13 | 14 | middleware.parse = function(opts, ctx) { 15 | if (!ctx) { 16 | ctx = opts 17 | opts = {} 18 | } 19 | 20 | return function(done) { 21 | var form = opts instanceof formidable.IncomingForm 22 | ? opts 23 | : new formidable.IncomingForm(opts) 24 | form.parse(ctx.req, function(err, fields, files) { 25 | if (err) return done(err) 26 | done(null, { fields: fields, files: files }) 27 | }) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "koa-formidable", 3 | "author": { 4 | "name": "Markus Ast", 5 | "email": "npm.m@rkusa.st" 6 | }, 7 | "version": "1.0.0", 8 | "description": "Formidable middleware for Koa", 9 | "keywords": [ 10 | "koa", 11 | "formidable", 12 | "form", 13 | "upload", 14 | "multipart", 15 | "bodyparser" 16 | ], 17 | "homepage": "https://github.com/rkusa/koa-formidable", 18 | "dependencies": { 19 | "formidable": "~1.0.14" 20 | }, 21 | "bugs": "https://github.com/rkusa/koa-formidable/issues", 22 | "repository": { 23 | "type": "git", 24 | "url": "git://github.com/rkusa/koa-formidable.git" 25 | }, 26 | "engines": { 27 | "node": ">=0.8" 28 | } 29 | } 30 | --------------------------------------------------------------------------------