• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

egg.js 间件的使用

武飞扬头像
皮阿玛
帮助1

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

config.default.js

  1.  
    // 配置中间件
  2.  
    config.middleware = ['auth'];
  3.  
     
  4.  
     
  5.  
    //给中间件传参
  6.  
     
  7.  
     
  8.  
    config.auth={
  9.  
     
  10.  
    title:'this is auth111'
  11.  
    }

middleware auth.js

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

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

路由中直接引用 调用

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

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

koa-compress   

  1.  
    const compress = require('koa-compress')
  2.  
    const Koa = require('koa')
  3.  
     
  4.  
    const app = new Koa()
  5.  
    app.use(compress({
  6.  
    filter (content_type) {
  7.  
    return /text/i.test(content_type)
  8.  
    },
  9.  
    threshold: 2048,
  10.  
    gzip: {
  11.  
    flush: require('zlib').constants.Z_SYNC_FLUSH
  12.  
    },
  13.  
    deflate: {
  14.  
    flush: require('zlib').constants.Z_SYNC_FLUSH,
  15.  
    },
  16.  
    br: false // disable brotli
  17.  
    }))

egg使用koa   middleware compress.js

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

参数到配置文件中添加

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

2.koa-ip

  1.  
    const app = new Koa()
  2.  
    app.use((ctx, next) => {
  3.  
    ctx.request.ip = '127.0.0.1'
  4.  
    return next()
  5.  
    })
  6.  
    app.use(ip({
  7.  
    blacklist: ['127.0.0.*'],
  8.  
    handler: async (ctx, next) => {
  9.  
    ctx.status = 403
  10.  
    }
  11.  
    }))

egg middleware kip.js

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

参数到配置文件中添加

  1.  
    config.kip={
  2.  
    blacklist: ['192.168.0.114','127.0.0.1'],
  3.  
    handler: async (ctx, next) => {
  4.  
    ctx.status = 403,
  5.  
    await ctx.render('403')
  6.  
    }
  7.  
    }

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

  1.  
    <!doctype html>
  2.  
    <html>
  3.  
    <head>
  4.  
    <meta charset="utf-8">
  5.  
    <title>403禁止页面模板</title>
  6.  
     
  7.  
     
  8.  
    <style>
  9.  
    @import url("https://fonts.谷歌apis.com/css?family=Share Tech Mono|Montserrat:700");
  10.  
     
  11.  
    * {
  12.  
    margin: 0;
  13.  
    padding: 0;
  14.  
    border: 0;
  15.  
    font-size: 100%;
  16.  
    font: inherit;
  17.  
    vertical-align: baseline;
  18.  
    box-sizing: border-box;
  19.  
    color: inherit;
  20.  
    }
  21.  
     
  22.  
    body {
  23.  
    background-image: linear-gradient(120deg, #4f0088 0%, #000000 100%);
  24.  
    height: 100vh;
  25.  
    }
  26.  
     
  27.  
    h1 {
  28.  
    font-size: 45vw;
  29.  
    text-align: center;
  30.  
    position: fixed;
  31.  
    width: 100vw;
  32.  
    z-index: 1;
  33.  
    color: #ffffff26;
  34.  
    text-shadow: 0 0 50px rgba(0, 0, 0, 0.07);
  35.  
    top: 50%;
  36.  
    -webkit-transform: translateY(-50%);
  37.  
    transform: translateY(-50%);
  38.  
    font-family: "Montserrat", monospace;
  39.  
    }
  40.  
     
  41.  
    div {
  42.  
    background: rgba(0, 0, 0, 0);
  43.  
    width: 70vw;
  44.  
    position: relative;
  45.  
    top: 50%;
  46.  
    -webkit-transform: translateY(-50%);
  47.  
    transform: translateY(-50%);
  48.  
    margin: 0 auto;
  49.  
    padding: 30px 30px 10px;
  50.  
    box-shadow: 0 0 150px -20px rgba(0, 0, 0, 0.5);
  51.  
    z-index: 3;
  52.  
    }
  53.  
     
  54.  
    P {
  55.  
    font-family: "Share Tech Mono", monospace;
  56.  
    color: #f5f5f5;
  57.  
    margin: 0 0 20px;
  58.  
    font-size: 17px;
  59.  
    line-height: 1.2;
  60.  
    }
  61.  
     
  62.  
    span {
  63.  
    color: #f0c674;
  64.  
    }
  65.  
     
  66.  
    i {
  67.  
    color: #8abeb7;
  68.  
    }
  69.  
     
  70.  
    div a {
  71.  
    text-decoration: none;
  72.  
    }
  73.  
     
  74.  
    b {
  75.  
    color: #81a2be;
  76.  
    }
  77.  
     
  78.  
    a.avatar {
  79.  
    position: fixed;
  80.  
    bottom: 15px;
  81.  
    right: -100px;
  82.  
    -webkit-animation: slide 0.5s 4.5s forwards;
  83.  
    animation: slide 0.5s 4.5s forwards;
  84.  
    display: block;
  85.  
    z-index: 4
  86.  
    }
  87.  
     
  88.  
    a.avatar img {
  89.  
    border-radius: 100%;
  90.  
    width: 44px;
  91.  
    border: 2px solid white;
  92.  
    }
  93.  
     
  94.  
    @-webkit-keyframes slide {
  95.  
    from {
  96.  
    right: -100px;
  97.  
    -webkit-transform: rotate(360deg);
  98.  
    transform: rotate(360deg);
  99.  
    opacity: 0;
  100.  
    }
  101.  
    to {
  102.  
    right: 15px;
  103.  
    -webkit-transform: rotate(0deg);
  104.  
    transform: rotate(0deg);
  105.  
    opacity: 1;
  106.  
    }
  107.  
    }
  108.  
     
  109.  
    @keyframes slide {
  110.  
    from {
  111.  
    right: -100px;
  112.  
    -webkit-transform: rotate(360deg);
  113.  
    transform: rotate(360deg);
  114.  
    opacity: 0;
  115.  
    }
  116.  
    to {
  117.  
    right: 15px;
  118.  
    -webkit-transform: rotate(0deg);
  119.  
    transform: rotate(0deg);
  120.  
    opacity: 1;
  121.  
    }
  122.  
    }
  123.  
    </style>
  124.  
    </head>
  125.  
    <body>
  126.  
    <h1>403</h1>
  127.  
    <div><p>> <span>ERROR CODE</span>: "<i>HTTP 403 Forbidden</i>"</p>
  128.  
    <p>> <span>ERROR DESCRIPTION</span>: "<i>Access Denied. You Do Not Have The Permission To Access This Page On This Server</i>"</p>
  129.  
    <p>> <span>ERROR POSSIBLY CAUSED BY</span>: [<b>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</b>...]</p>
  130.  
    <p>> <span>SOME PAGES ON THIS SERVER THAT YOU DO HAVE PERMISSION TO ACCESS</span>: [<a href="/">Home Page</a>, <a href="/">About Us</a>, <a href="/">Contact Us</a>, <a href="/">Blog</a>...]</p><p>> <span>HAVE A NICE DAY SIR AXLEROD :-)</span></p>
  131.  
    </div>
  132.  
     
  133.  
     
  134.  
    <script>
  135.  
    var str = document.getElementsByTagName('div')[0].innerHTML.toString();
  136.  
    var i = 0;
  137.  
    document.getElementsByTagName('div')[0].innerHTML = "";
  138.  
     
  139.  
    setTimeout(function() {
  140.  
    var se = setInterval(function() {
  141.  
    i ;
  142.  
    document.getElementsByTagName('div')[0].innerHTML = str.slice(0, i) "|";
  143.  
    if (i == str.length) {
  144.  
    clearInterval(se);
  145.  
    document.getElementsByTagName('div')[0].innerHTML = str;
  146.  
    }
  147.  
    }, 10);
  148.  
    },0);
  149.  
    </script>
  150.  
     
  151.  
    </body>
  152.  
    </html>

4.使用koa非规范中间件 

koa

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

egg

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

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

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

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

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhiaeabi
系列文章
更多 icon
同类精品
更多 icon
继续加载