之前定義路由一般是這么用的:router.get('/',function(ctx,next){ctx.body='hellohello';})最近看到了一種用法,在方法參數(shù)中加入了一個中間件:router.get('/',(ctx,next)=>{returnUser.findOne(ctx.params.id).then(function(user){ctx.user=user;next();});},ctx=>{console.log(ctx.user);//=>{id:17,name:"Alex"}});為什么能這樣用,難道說router.get的方法定義不是死的嗎?如何做到中間可以支持中間件的嗎?另外中間件的注冊不是一般都是app.use(),為什么在這里也能用中間件?
新手上路,koa-router內部使用中間件求老司機指點!
慕慕森
2019-09-08 22:24:15