Chinaunix首页 | 论坛 | 博客
  • 博客访问: 54455
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-12 11:48
文章分类
文章存档

2016年(48)

我的朋友

分类: JavaScript

2016-11-03 12:05:21

koahub-skip

koahub skip middleware

Conditionally skip a middleware when a condition is met.

With existing middlewares:

点击(此处)折叠或打开

  1. var skip = require('koahub-skip');
  2. var serve = require('koa-static');
  3.  
  4. var static = serve(__dirname + '/public');
  5. static.skip = skip;
  6.  
  7. app.use(static.skip({ method: 'OPTIONS' }));


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

点击(此处)折叠或打开

  1. module.exports = function () {
  2.   var mymid = function *(next) {
  3.     // Do something
  4.   };
  5.  
  6.   mymid.skip = require('koahub-skip');
  7.  
  8.   return mymid;
  9. };


  • 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 ctx.url instead of ctx.originalUrl.

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

点击(此处)折叠或打开

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

 

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

点击(此处)折叠或打开

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

 

wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统

wemall地址:
代码地址:

阅读(407) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~