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