1. 方式一:在路由里配置
- router.js
module.exports = (app) => {
const { router, controller, io } = app;
const userAuth = app.middleware.userauth(app);
router.get(‘/user/info‘, userAuth, controller[‘user‘].info);
};
2. 方式二:在 config.default.js 里配置
- config.default.js
module.exports = (appInfo) => {
config.middleware = [‘userauth‘];
config.userauth = {
enable: true, // 是否开启该中间件,默认 true
match: [‘/user‘], // 匹配路由
// ignore: [‘/home‘] // 忽略路由,与 match 不能共用
};
}
或者:
config.userauth = {
enable: true,
match(ctx) {
const { url } = ctx.request;
if (url === ‘/‘) return false;
const excludes = [‘/home‘, ‘/user/signUp‘, ‘/user/signIn‘];
return !excludes.some((item) => url.startsWith(item));
},
};