Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-body

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-body - npm Package Compare versions

Comparing version 2.0.1 to 2.1.0

4

package.json
{
"name": "koa-body",
"version": "2.0.1",
"version": "2.1.0",
"description": "A koa body parser middleware. Support multipart, urlencoded and json request bodies.",

@@ -42,3 +42,3 @@ "main": "index.js",

"co-body": "*",
"formidable": "1.0.17"
"formidable": "1.1.1"
},

@@ -45,0 +45,0 @@ "devDependencies": {

@@ -28,22 +28,35 @@ koa-body [![Build Status](https://travis-ci.org/dlau/koa-body.svg?branch=koa2)](https://travis-ci.org/dlau/koa-body) [![Dependencies Status](https://david-dm.org/dlau/koa-body/status.svg)](https://david-dm.org/dlau/koa-body)

## Hello world
```sh
npm install koa
npm install koa-body
nvm install v7.9.0 #Note - koa requires node v7.6.0 for ES2015/async support
```
index.js:
```js
const Koa = require('koa');
const koaBody = require('koa-body');
## Usage like [multer](https://github.com/expressjs/multer)
> It's very simple, because you can access the fields and files in the `ctx.request.body` or `ctx.req.body` JSON object
const app = new Koa();
```js
var app = require('koa')(),
koaBody = require('koa-body');
app.use(koaBody());
app.use(ctx => {
ctx.body = `Request Body: ${JSON.stringify(ctx.request.body)}`;
});
app.use(koaBody({formidable:{uploadDir: __dirname}}));
app.use((ctx, next) => {
if (ctx.request.method == 'POST') {
console.log(ctx.request.body);
// => POST body
ctx.body = JSON.stringify(ctx.request.body);
}
next()
});
app.listen(3131)
console.log('curl -i http://localhost:3131/ -d "name=test"');
app.listen(3000);
```
```sh
$ node index.js
$ curl -i http://localhost:3000/users -d "name=test"
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: 29
Date: Wed, 03 May 2017 02:09:44 GMT
Connection: keep-alive
Request Body: {"name":"test"}%
```
**For a more comprehensive example, see** `examples/multipart.js`

@@ -55,8 +68,9 @@

```js
var app = require('koa')(),
router = require('koa-router')(),
koaBody = require('koa-body')();
const Koa = require('koa');
const app = new Koa();
const router = require('koa-router')();
const koaBody = require('koa-body')();
router.post('/users', koaBody,
(ctx, next) => {
(ctx) => {
console.log(ctx.request.body);

@@ -70,4 +84,4 @@ // => POST body

app.listen(3131);
console.log('curl -i http://localhost:3131/users -d "name=test"');
app.listen(3000);
console.log('curl -i http://localhost:3000/users -d "name=test"');
```

@@ -102,3 +116,3 @@

- `maxFields` **{Integer}** Limits the number of fields that the querystring parser will decode, default `1000`
- `maxFieldsSize` **{Integer}** Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted, default `2mb (2 * 2 * 1024)`
- `maxFieldsSize` **{Integer}** Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted, default `2mb (2 * 1024 * 1024)`
- `uploadDir` **{String}** Sets the directory for placing file uploads in, default `os.tmpDir()`

@@ -105,0 +119,0 @@ - `keepExtensions` **{Boolean}** Files written to `uploadDir` will include the extensions of the original files, default `false`

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc