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 0.0.4 to 0.1.0

example.js

63

index.js

@@ -0,29 +1,56 @@

/*!
* koa-body
* Copyright (c) 2014. Daryl Lau (@daryllau), Charlike Mike Reagent (@tunnckoCore)
* MIT LICENSE
*/
'use strict';
var co_body = require('co-body');
module.exports = function(opts){
var patchNode = (opts && 'patchNode' in opts) ? opts.patchNode : false;
var patchKoa = (opts && 'patchKoa' in opts) ? opts.patchKoa : true;
/**
* Module dependencies.
*/
var co_body = require('co-body');
/**
* Expose `requestbody()`.
*/
module.exports = requestbody;
/**
* Initialize module middleware with the given `options`:
* - `jsonLimit` limits application/json request body, co-body's option
* - `formLimit` limits application/x-www-form-urlencoded request body, co-body's option
* - `patchNode` Set the body parameter in the **Node** request object `this.req.body`
* - `patchKoa` Set the body parameter in the **Koa** request object `this.request.body`
* - `encoding` request encoding, co-body's option
*
* @param {Object} options
* @return {Function}
* @api public
*/
function requestbody(opts) {
var jsonLimit = (opts && 'jsonLimit' in opts) ? opts.jsonLimit : '1mb',
formLimit = (opts && 'formLimit' in opts) ? opts.formLimit : '56kb',
patchNode = (opts && 'patchNode' in opts) ? opts.patchNode : false,
patchKoa = (opts && 'patchKoa' in opts) ? opts.patchKoa : true,
encoding = (opts && 'encoding' in opts) ? opts.encoding : 'utf-8';
return function *(next){
var body;
if(this.is('application/json')){
body = yield co_body.json(this);
if(patchNode){
this.req.body = body;
}
if(patchKoa){
this.request.body = body;
}
body = yield co_body.json(this,{encoding: encoding, limit: jsonLimit});
}
else if(this.is('application/x-www-form-urlencoded')){
body = yield co_body.form(this);
if(patchNode){
this.req.body = body;
}
if(patchKoa){
this.request.body = body;
}
body = yield co_body.form(this,{encoding: encoding, limit: formLimit});
}
if(patchNode){
this.req.body = body;
}
if(patchKoa){
this.request.body = body;
}
yield next;
};
};
{
"name": "koa-body",
"version": "0.0.4",
"version": "0.1.0",
"description": "koa middleware that parses request body",
"main": "index.js",
"files": [
"index.js",
"example.js",
"test.js",
"README.md",
"LICENSE",
"Makefile",
".gitignore",
".npmignore"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"start": "make start",
"test": "make test"
},
"repository": {
"type": "git",
"url": "git://github.com/dlau/koa-body"
"url": "https://github.com/dlau/koa-body"
},
"keywords": [
"koa",
"request",
"middleware",
"body",
"request",
"parse"
"bodyparser",
"parse",
"json",
"bodyjson",
"parsejson",
"parseform",
"form"
],
"author": "Daryl Lau",
"license": "MIT",
"bugs": {
"url": "https://github.com/dlau/koa-body/issues"
"dependencies": {
"co-body": "*"
},
"homepage": "https://github.com/dlau/koa-body",
"dependencies": {
"co-body": "0.0.1"
}
"devDependencies": {
"koa": "*",
"mocha": "*",
"should": "*",
"supertest": "*"
},
"contributors": [
{
"name": "Daryl Lau"
},
{
"name": "Charlike Mike Reagent"
}
],
"author": "Daryl Lau (http://weak.io/)",
"license": "MIT"
}

@@ -0,10 +1,12 @@

koa-body [![Build Status](https://travis-ci.org/dlau/koa-body.png)](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)
================
# koa-body
KoaJS middleware that patch body (to Koa#req.body or Node#request.body, or bother) and parse body to json/form with co-body.
[koa](https://github.com/koajs/koa) middleware for parsing a request body.
This is a simple wrapper around [co-body](https://github.com/co/co-body). Provides similar functionality to the express's [request body parser](http://expressjs.com/api.html#req.body)
[koa](https://github.com/koajs/koa) middleware for parsing a request body. This is a simple wrapper around [co-body](https://github.com/co/co-body). Provides similar functionality to the express's [request body parser](http://expressjs.com/api.html#req.body)
Doesn't support multipart. [Discuss](https://github.com/dlau/koa-body/issues/1)
Doesn't support multipart
## Installation
## Install
```

@@ -14,28 +16,96 @@ $ npm install koa-body

## Options
- patchNode
Set the body parameter in the node request object `this.req.body`
Initialize koa-better-body middleware with the given `options`:
- **patchNode** Set the body parameter in the **Node** request object `this.req.body`
*defauls to false*
- patchKoa
Set the body parameter in the koa request object `this.request.body`
- **patchKoa** Set the body parameter in the **Koa** request object `this.request.body`
*defaults to true*
- **jsonLimit** limits application/json request body, co-body's option
*defauls to '1mb'*
- **formLimit** limits application/x-www-form-urlencoded request body, co-body's option
*defauls to '56kb'*
- **encoding** request encoding, co-body's option
*defauls to 'utf-8'*
## Example
## Usage
```js
var koa = require('koa');
var koa_body = require('koa-body');
var app = koa();
var app = require('koa')()
, betterBody = require('./index');
/**
* By default body is patching to
* Koa's ctx.request
*/
app.use(betterBody({patchNode: false, jsonLimit: '1kb', formLimit: '1kb'}));
app.use(koa_body());
app.use(function *(next){
var patchKoa = (this.request.body) ? this.request.body : 'patchKoa=true, by default';
var patchNode = (this.req.body) ? this.req.body : 'patchNode=false, by default';
app.use(function *(){
console.log(this.request.body);
if (this.request.method == 'POST') {
this.status = 201;
} else if (this.request.method == 'GET') {
this.status = 200
}
this.body = JSON.stringify({koa: patchKoa, node: patchNode});
if (this.request.method == 'PUT') {
this.status = 200;
this.body = 'resource updated successfully';
}
if (this.request.method == 'DELETE') {
this.status = 204;
this.body = 'resource deleted successfully';
}
});
var port = process.env.PORT || 3333;
app.listen(port);
console.log('Koa server start listening to port '+port);
```
# License
## Usage with [koa-router](https://github.com/alexmingoia/koa-router)
> It's generally better to only parse the body as needed, if using a router that supports middleware composition, we can inject it only for certain routes.
MIT
```js
var app = require('koa')(),
router = require('koa-router'),
koaBody = require('koa-body')();
app.use(router());
app.post('/users', koaBody,
function *(next) {
console.log(this.request.body);
// => POST body
}
);
```
## Features
- 14 passing (174ms) tests
- co-body options: jsonLimit, formLimit, encoding
- freedom to choose - patch body to
* KoaJS Context `this.request.body`
* NodeJS Context `this.req.body`
- only 1 dependency: `co-body`
## Test, Bench, Example
First run `npm install` before run anything.
```
npm test
npm start
```
## Credit
|[![Daryl Lau](https://avatars2.githubusercontent.com/u/2764274?s=144)](https://github.com/dlau)| [![Charlike Mike Reagent](https://avatars2.githubusercontent.com/u/5038030?s=144)](https://github.com/tunnckoCore)|
|---|---|
|[Daryl Lau](https://github.com/dlau) (creator) | [George Yanev](https://github.com/tunnckoCore) (contrib)|
## LICENSE
The MIT License, 2014 [Daryl Lau](http://weak.io) ([@daryllau](https://twitter.com/tunnckoCore)), [Charlike Mike Reagent](https://github.com/tunnckoCore) ([@tunnckoCore](https://twitter.com/tunnckoCore))

Sorry, the diff of this file is not supported yet

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