We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
koa是一个web框架,比express轻量的多,koa给更像是一个中间件框架,koa只是一个基础的架子,需要用到的相应的功能时,可以用相应的中间件来实现,比如路由中间件、日志中间件等。 AOP即面向切面编程。就是在现有的代码程序中,在程序生命周期或者横向流程中加入/减去一个或多个功能,不影响原有功能。
如下Logger功能,可以拆分成一个独立函数。
const logger = (ctx, next) => { console.log(`${Date.now()} ${ctx.request.method} ${ctx.request.url}`); next(); } app.use(logger);
上面代码中的logger函数就叫做"中间件"(middleware),因为它处在 HTTP Request 和 HTTP Response 中间,用来实现某种中间功能。app.use()用来加载中间件。
const one = (ctx, next) => { console.log('>> one'); next(); console.log('<< one'); } const two = (ctx, next) => { console.log('>> two'); next(); console.log('<< two'); } const three = (ctx, next) => { console.log('>> three'); next(); console.log('<< three'); } app.use(one); app.use(two); app.use(three);
执行结果为:
>> one >> two >> three << three << two << one
http请求=>中间件处理=>http响应 中间件处理包含路由操作、权限处理、数据安全、数据操作、数据查询等。
[1]https://chenshenhai.github.io/koajs-design-note/note/chapter02/03.html [2]https://hijiangtao.github.io/2017/11/10/Mastering-Koa-Middleware/ [3]https://cnodejs.org/topic/58fd8ec7523b9d0956dad945 [4]http://www.ruanyifeng.com/blog/2017/08/koa.html [5]https://www.jianshu.com/p/02ed208d4577
The text was updated successfully, but these errors were encountered:
No branches or pull requests
koa的AOP设计
koa是一个web框架,比express轻量的多,koa给更像是一个中间件框架,koa只是一个基础的架子,需要用到的相应的功能时,可以用相应的中间件来实现,比如路由中间件、日志中间件等。
AOP即面向切面编程。就是在现有的代码程序中,在程序生命周期或者横向流程中加入/减去一个或多个功能,不影响原有功能。
koa的切面
中间件概念
如下Logger功能,可以拆分成一个独立函数。
上面代码中的logger函数就叫做"中间件"(middleware),因为它处在 HTTP Request 和 HTTP Response 中间,用来实现某种中间功能。app.use()用来加载中间件。
中间件执行顺序
例如:
执行结果为:
一次http请求过程
http请求=>中间件处理=>http响应
中间件处理包含路由操作、权限处理、数据安全、数据操作、数据查询等。
[1]https://chenshenhai.github.io/koajs-design-note/note/chapter02/03.html
[2]https://hijiangtao.github.io/2017/11/10/Mastering-Koa-Middleware/
[3]https://cnodejs.org/topic/58fd8ec7523b9d0956dad945
[4]http://www.ruanyifeng.com/blog/2017/08/koa.html
[5]https://www.jianshu.com/p/02ed208d4577
The text was updated successfully, but these errors were encountered: