egg.js 中间件的使用

1.路由中间件  就是任意路由都触发的中间件

config.default.js

  // 配置中间件
  config.middleware = ['auth'];


  //给中间件传参


  config.auth={

    title:'this is auth111'
  }

middleware auth.js

module.exports = (option, app) => {
    return async function auth(ctx, next) {
        console.log(option);
        //实现中间件的功能
        console.log(new Date());
        await next();
    }
}

2.给指定路由使用中间件  配置文件中不再配置中间件

路由中直接引用 调用

 //路由中获取中间件
  var auth=app.middleware.auth({title:'this is router.js  middleware'})
  router.get('/',auth, controller.home.index);  

3.使用koa 规范中间件 即 app.use(xxx({xxx}))形式调用   参数为对象

koa-compress   

const compress = require('koa-compress')
const Koa = require('koa')

const app = new Koa()
app.use(compress({
  filter (content_type) {
  	return /text/i.test(content_type)
  },
  threshold: 2048,
  gzip: {
    flush: require('zlib').constants.Z_SYNC_FLUSH
  },
  deflate: {
    flush: require('zlib').constants.Z_SYNC_FLUSH,
  },
  br: false // disable brotli
}))

egg使用koa   middleware compress.js

module.exports=require('koa-compress');

参数到配置文件中添加

 config.middleware = ['forbidip','compress','kip'];
  config.compress={
    threshold: 1024 //它支持指定只有当 body 大于配置的 threshold 时才进行 gzip 压缩
  }

2.koa-ip

const app = new Koa()
app.use((ctx, next) => {
  ctx.request.ip = '127.0.0.1'
  return next()
})
app.use(ip({
  blacklist: ['127.0.0.*'],
  handler: async (ctx, next) => {
    ctx.status = 403
  }
}))

egg middleware kip.js

module.exports=require('koa-ip');

参数到配置文件中添加

  config.kip={
    blacklist: ['192.168.0.114','127.0.0.1'],
    handler: async (ctx, next) => {
      ctx.status = 403,
      await ctx.render('403')
    }
  }

炫酷的动态403模板 可在https://www.mk2048.com/免费下载模板





403禁止页面模板





403

> ERROR CODE: "HTTP 403 Forbidden"

> ERROR DESCRIPTION: "Access Denied. You Do Not Have The Permission To Access This Page On This Server"

> ERROR POSSIBLY CAUSED BY: [execute access forbidden, read access forbidden, write access forbidden, ssl required, ssl 128 required, ip address rejected, client certificate required, site access denied, too many users, invalid configuration, password change, mapper denied access, client certificate revoked, directory listing denied, client access licenses exceeded, client certificate is untrusted or invalid, client certificate has expired or is not yet valid, passport logon failed, source access denied, infinite depth is denied, too many requests from the same client ip...]

> SOME PAGES ON THIS SERVER THAT YOU DO HAVE PERMISSION TO ACCESS: [Home Page, About Us, Contact Us, Blog...]

> HAVE A NICE DAY SIR AXLEROD :-)

4.使用koa非规范中间件 

koa

  const Middleware = require('some-koa-middleware');
    app.use(Middleware(options.compiler,options.xxxx))

egg

const Middleware = require('some-koa-middleware');
module.exports=(option,app)=>{
    return Middleware(options.compiler,options.xxxx);
}

5.通用中间件  就是再中间件配置文件中添加属性方法 

  • enable:控制中间件是否开启。
  • match:设置只有符合某些规则的请求才会经过这个中间件。
  • ignore:设置符合某些规则的请求不经过这个中间件。match相反  只可定义其一
  config.kip={
    blacklist: ['192.168.0.114','127.0.0.1'],
    match:'/he',
    enable:true,
    match(ctx) {
      // 只有 ios 设备才开启
      const reg = /iphone|ipad|ipod/i;
      return reg.test(ctx.get('user-agent'));
    },
    handler: async (ctx, next) => {
      ctx.status = 403,
      await ctx.render('403')
    }
  }

比如指定中间件使用,一是可以直接使用调用方式  二 配置文件中watch下路由或者通过方法定义规则

注意  中间件命名 若是 user_auth.js  再配置文件或者js代码中必须写为userAuth

你可能感兴趣的:(eggjs,node.js)