eggjs 路由中间件完整demo

文章为博主原创 博主wx: xuelangzhiye ,转载附上本微博链接,谢谢.

写个demo  方便新手理解eggjs 中间件的操作

首先中间件可以应用在   router、框架、插件

router路由上的案例   原理  用户请求 ->路由中间件->控制器

1、新建一个中间件  isAuth.js     

app/middleware/isAuth.js


module.exports=option=>{
	return async function isAuth(ctx, next){
		if(ctx.cookies.get("userinfo")){//判断有没有userinfo的cookies
			await next();//放行
		}else{
			ctx.body="你没有登录"
		}
	}
}

2、全局引用  

/config/config.default.js

config.middleware= [
	'isAuth' 
];
 //config.isAuth= {
	//         enable: 

猜你喜欢

转载自blog.csdn.net/xuelang532777032/article/details/112863205
今日推荐