1、通用性中间件

1-1 app/middleware

  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'];

2、中间件的局部匹配

2-1 config/config.default.js

  1. config.middleware = ['auth','test'];
  2. // 让test中间件只匹配music
  3. config.test = {
  4. match:"/music",
  5. title:"中间件"
  6. }
  7. tips:test中间件只会匹配music路由

3、配置跨域

  1. module.exports = () => {
  2. return async function(ctx,next){
  3. ctx.set("Access-Control-Allow-Origin","*")
  4. await next()
  5. }
  6. }
  7. config.middleware = ['auth','test','cors'];