一、通用型中间件
1-1 app/middleware/auth.js
module.exports = ()=>{
return async function(ctx,next){
console.log(1);
await next();
}
}
1-2 配置中间件config/config.default.js
config.middleware = ['auth'];
二、中间件的局部匹配
config/config.default.js
config.middleware = ['auth'];
config.auth = {
match:'/music'
}
# tips:auth中间件只会匹配music路由
三、 配置跨域中间件
1-1 app/middleware/cors.js
module.exports = ()=>{
return async function(ctx,next){
ctx.set("Access-Control-Allow-Origin","*")
await next();
}
}
1-2 config/config.default.js
config.middleware = ["cors"];