一、通用型中间件

1-1 app/middleware/auth.js

  1. module.exports = ()=>{
  2. return async function(ctx,next){
  3. console.log(1);
  4. await next();
  5. }
  6. }

1-2 配置中间件config/config.default.js

  1. config.middleware = ['auth'];

二、中间件的局部匹配

config/config.default.js

  1. config.middleware = ['auth'];
  2. config.auth = {
  3. match:'/music'
  4. }
  5. # tips:auth中间件只会匹配music路由

三、 配置跨域中间件

1-1 app/middleware/cors.js

  1. module.exports = ()=>{
  2. return async function(ctx,next){
  3. ctx.set("Access-Control-Allow-Origin","*")
  4. await next();
  5. }
  6. }

1-2 config/config.default.js

  1. config.middleware = ["cors"];