1.定义一个中间件在应用中的基础使用
1.1定义中间件
//app/middleware/auth.js
module.exports = (option,app)=>{
return async function auth(ctx,next){
/* 实现中间件 */
console.log(new Date());
await next()
}
}
1.2配置中间件
//config/config.default.js
// 增加配置中间件
config.middleware = ['auth'];
1.3给中间件传参
// 增加配置中间件
config.middleware = ['auth'];
config.auth = {
/* 传递给中间件的option */
title:"this is middleware-param"
}
使用
//auth.js
module.exports = (option,app)=>{
return async function auth(ctx,next){
/* 实现中间件的参数 */
console.log(option.title)
await next()
}
}