Koa2: how to write chain of middleware?

。_饼干妹妹 提交于 2019-12-06 07:28:53

If you're simply interested in making sure a middlware runs for every route, all you have to do is register the middleware before you register your routing middelware.

app.use(middleware);

As long as you call this before you 'use' your router, it will be called for every request. Just make sure you call the next function. This is how your middleware might look like:

function middleware(ctx, next) {

   // Authenticate user

   // Eventually call this
   return next();

}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!