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

Foxandxss/koa-unless: Conditionally add a middleware to koa with some common pat ...

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

开源软件名称:

Foxandxss/koa-unless

开源软件地址:

https://github.com/Foxandxss/koa-unless

开源编程语言:

JavaScript 98.9%

开源软件介绍:

Koa Unless

Build Status

Conditionally skip a middleware when a condition is met.

Install

npm i koa-unless --save

Usage

With existing middlewares:

var unless = require('koa-unless');
var serve  = require('koa-static');

var static = serve(__dirname + '/public');
static.unless = unless;

app.use(static.unless({ method: 'OPTIONS' }));

If you are authoring a middleware you can support unless as follow:

module.exports = function () {
  var mymid = function *(next) {
	// Do something
  };

  mymid.unless = require('koa-unless');

  return mymid;
};

Current options

  • method it could be an string or an array of strings. If the request method match the middleware will not run.
  • path it could be an string, a regexp or an array of any of those. If the request path match, the middleware will not run.
  • ext it could be an string or an array of strings. If the request path ends with one of these extensions the middleware will not run.
  • custom it must be a function that returns true / false. If the function returns true for the given request, ithe middleware will not run. The function will have access to Koa's context via this
  • useOriginalUrl it should be true or false, default is true. if false, path will match against this.url instead of this.originalUrl.

Examples

Require authentication for every request unless the path is index.html.

app.use(requiresAuth.unless({ path: ['/index.html', '/'] }))

Avoid a fstat for request to routes doesnt end with a given extension.

app.use(static.unless(function () {
  var ext = url.parse(this.originalUrl).pathname.substr(-4);
  return !~['.jpg', '.html', '.css', '.js'].indexOf(ext);
}));

Credits

All the credits for this library goes for José F. Romaniello which created the original express version.

License

MIT 2015 - Jesús Rodríguez




鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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