• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

xpepermint/koa-strong-params: Rails-style implementation of strong parameters fo ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

xpepermint/koa-strong-params

开源软件地址:

https://github.com/xpepermint/koa-strong-params

开源编程语言:

JavaScript 100.0%

开源软件介绍:

koa-strong-params

Build Status NPM version Dependency Status

Rails-style implementation of strong parameters for Koa. The middleware adds the this.params object to the Koa context which returns an object, built from query string and request body data. The returned object has some useful methods allows for data requiring and filtering.

You should consider using koa-qs and koa-bodyparser packages together with koa-strong-params.

Installation

Install the npm package.

npm install koa-strong-params --save

Attach the middleware.

var koa = require('koa');
var params = require('koa-strong-params');
var app = koa();
app.use(params());

Example

var koa = require('koa');
var params = require('koa-strong-params');
var bodyparser = require('koa-bodyparser');
var qs = require('koa-qs')
var app = koa();
qs(app); // required for nested query string objects
app.use(bodyparser()); // required for params to include request body objects
app.use(params());
app.use(function *() {

  // all available params
  this.body = this.params.all();
  // -> { id: '13', name: 'Bob', age: '13', email: '[email protected]', address: { country: 'US', street: '261 West' }}

  // only selected params
  this.body = this.params.only('name', 'age');
  // -> { name: 'Bob', age: '13' }

  // all params except those provided
  this.body = this.params.except('name', 'id', 'address');
  // -> { id: '13', age: '13', email: '[email protected]' }

  // all params of a sub-object
  this.body = this.params.require('address').all();
  // -> { country: 'US', street: '261 West' }

  // only selected params + some merged attributes
  this.body = this.params.merge({ badge: 'coder' }).only('name');
  // -> { name: 'Bob', badge: 'coder' }

});
app.listen(3001);



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
paramsinghvc/koa-react-spa发布时间:2022-07-09
下一篇:
koajs/koa-safe-jsonp: Safe jsonp plusins for koa.发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap